Module 1: Introduction
History
A brief history of C:
- Developed in 1972 at AT&T Bell Labs by Dennis Ritchie and Brian
Kernighan.
- Primary purpose: provide a higher-level programming language
than assembly in which to write Unix.
- Why the name C?
- C was preceded by B,
a language designed by Ken Thompson (another Unix designer).
- B was based on
BCPL, another high-level language.
- Classic book: "The C Programming Language" by Kernighan and
Ritchie.
- Since then:
- C has gone through many changes.
- Original C is called "K&R C".
- ANSI defined a standard in 1989, now called ANSI C89.
- Most common: update in 1999 called ANSI C99.
- Current: 2011, called ANSI C11.
- See
The Development of the C Language, a history of C written by
Dennis Ritchie.
Why learn C?
Highlights of C:
- C has many high-level programming features, but also let's you work
at the lowest level (manipulating bits).
- C is amenable to compiler optimization: compiled C code is
generally very efficient.
- C gives the programmer a lot of freedom in accessing memory
and hardware.
- Most operating systems, embedded systems are written in C.
- A lot of open-source projects are written in C.
The world of C:
- Software development in C usually involves using additional
C-related tools.
- Tools for programming:
- Development environments, e.g., Eclipse, Emacs
- Debuggers, e.g. gdb
- Useful tools/libraries, e.g., lex and yacc.
- Tools for managing projects, e.g., make, ant.
- Tools for configuring software, e.g., autoconf.
- Tools for GUI development: tk/tcl,
QT, GTK.
- Scripting languages: perl, awk.
Open-source software:
- GNU-Linux is written in C, as are most tools from GNU.
- Most open-source projects today are written in C
- see www.sourceforge.net
- Open-source projects tend to use the gcc compiler
(itself an open-source project) and related tools.
Going from Java to C
For a Java programmer:
- C's syntax is similar.
- Large parts of C will appear to be a subset of Java.
- But ...
- C has pointers, with a lot of freedom to manipulate them.
- C has less type-checking than Java.
- C's variable declarations can look strange.
- C provides no support for objects.
- C has function declarations.
- C has a preprocessor language that's processed before compilation.
- The programming experience in C:
- Because of the additional freedom in the language, it's much
easier to create errors.
- There's less help from the compiler, and no help from the
runtime system.
- You need to use a debugger to track pointer errors.
- C's freedom in programming allows one to construct some
highly unreadable code. For example, here is one winning entry
from the International Obfuscated C Code Contest:
int i;main(){for(;i["] < i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
(It prints "Hello world!" to the screen).
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
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.
Important: Here is a common mistake to avoid:
- First, recall: the
-o
option names the executable file
gcc -o helloworld -std=c89 helloworld.c
- Here, the executable file is what we're asking the
compiler to name as
helloworld
- This is the file that we execute:
./helloworld
- Now, if one makes the mistake of typing
gcc -o helloworld.c -std=c89 helloworld.c
Then, the executable gets written over the file
helloworld.c
because that's what we're asking the compiler to do
(that is, we're telling it to write the executable code
into the file helloworld.c)
- This has the unfortunate effect of destroying the
original file helloworld.c.
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.
© 2003, Rahul Simha (revised 2017)