Module 1: Introduction


History

A brief history of C:


Why learn C?

Highlights of C:

The world of C:

Open-source software:


Going from Java to C

For a Java programmer:


HelloWorld in C

Here is the classic HelloWorld program in C: (source file)

#include <stdio.h>

int main ()
{
  printf ("Hello World!\n");
}
Note:
  • The #include command is a preprocessor directive to load the stdio library.

  • Execution starts in a function (method) called main:
    • There are other signatures for main as we will see.
    • Although a return type is declared, nothing needs to be returned.

  • The printf method is used for screen output.

  • The "newline" character \n is explicitly required.

Compiling and executing on Unix:

  • The program above is a plain text file, as in any programming language.

  • The file extension needs to be .c.

  • The file name need not be helloworld.

  • To compile:
      gcc -o helloworld -ansi helloworld.c
      

  • This produces an executable called helloworld which can be executed as:
      helloworld
      
    or, if you don't have the current directory in your path:
      ./helloworld
      
In-Class Exercise 1.1: Remove the newline character and see what happens.

Some variations:

  • The file name:
    • With the same contents, we could re-name the file to whatever.c and compile as:
          gcc -o helloworld -ansi whatever.c
          
      (Note the case-sensitivity).
    • We could also create an executable with any name:
          gcc -o strangeThing -ansi whatever.c
          

  • The compiler options (switches):
    • The simplest form on invoking the gcc C compiler is:
        gcc helloworld.c
        
      This produces an executable called a.out (by tradition).
    • The -o option lets you specify the name of the executable.
    • The -ansi option asks the compiler to enforce the current ANSI standard.
    • You can choose to compile with a specific (e.g., ANSI 1989) standard as follows:
        gcc -o helloworld -std=c99 helloworld.c
        

  • There are many other useful compiler options:
    • Two of the most useful are "debugging" and "math":
        gcc -g -o helloworld -ansi helloworld.c -lm
        
    • The "math" library is loaded using the -lm switch at the end of the line.
    • The debug option -g directs the compiler to produce an executable that can be run inside a debugger.
    • Other useful compiler options include: compilation for a specific machine and various compiler optimizations.

  • Running the program inside a debugger:
    • First, compile with with the debug option:
          gcc -g -o helloworld -ansi helloworld.c 
          
    • Then bring up the debugger:
          gdb helloworld
          
      and run the program:
          (gdb) run
          Hello World
          (gdb) quit
          
    • Note: some Linux configurations of the debugger have a slightly different behavior:
          (gdb) run
          % ./helloworld
          Hello World
          % exit
          (gdb) quit
          

  • Why run inside a debugger?
    • When a C program "crashes", the runtime system typically identifies one unhelpful error: a segmentation fault.
    • Inside a debugger, you can examine memory addresses and trap such runtime errors.
    • Debuggers also let you step through execution, examining variables as you go along.
In-Class Exercise 1.2: Let's add a comment to HelloWorld:
#include <stdio.h>

int main ()
{
  // Print string to screen.
  printf ("Hello World\n");
}
Now compile with both c89 and c99 options and see what you get. Explain the difference.


C-related languages

Languages related to C:

  • C++
    • C++ is "mostly" a superset of C, offering support for objects.
    • gcc can compile C++ as well - this is useful for projects that combine C and C++.

  • Objective C
    • Another C enhancement to support objects.
    • Popular on Apple/MAC platforms.

  • C#
    • Microsoft's answer to Java.

  • Many other experimental variations: TinyC, Cyclone, D.