Programming in C © 2003 Kishori Mundargi
No rated * * * * * Resize -A   +A

CHAPTER 2:

Compiling a simple C program

This chapter covers some simple C programs which will help the student in understanding the structure of a C program. The student is encouraged to try as many programs as possible to become familiar with the syntax of the language and the types of errors encountered.

One important feature of C is, it is a case sensitive language. Lowercase and uppercase letters are distinct. So the C compiler interprets a variable name VAR as different from var or Var. Also unlike other languages like Fortran, in C it does not matter where you start typing your code. But it is advisable to follow coding conventions in order to develop programs which are readable and hence easily maintainable.

Consider the following program :


/* Program to display a string on the screen */

#include <:stdio.h>
void main()
  {
    printf("Hello World \n");
  }

Let us first understand the different parts of this C program. This program begins with a comment enclosed between /* and */. A comment is a note or explanation that you put into your source code. Comments are used primarily to document the meaning and purpose of your source code, so that you can remember later how it functions and how to use it. You can also use a comment to temporarily remove a line of code. Simply surround the lines with the comment symbols. Comments cannot be nested, but can span several lines. Generally they are placed at the begining of a program or function or they may co-exist on lines that have other code. Comments make a program readable and maintainable.

The second line of the program is a preprocessing directive. Preprocessors are discussed in subsequent chapters (see Chapter 14). This preprocessor tells the compiler to include the stdio.h header file. This is a standard header file that has functions and global values for console and file input and output. If the header file is supplied by the compiler system then enclose it with angle brackets (< >) , else if it is your own include file then surround it with double quotes (" "). More explanation can be found in reference material on standard C Library files.

main() is a special keyword that indicates to the C system the entry point of a program. This is where the program is to begin execution. The keyword void before main() indicates that the function main() does not return a value. The parentheses following main() indicate that no parameters or arguments are passed on to this function. The syntax of a function and its working will be explained in detail in Chapter 17 on Functions.

Following main, is the body of the program which consists of several statements enclosed within a pair of curly braces. In the above program we have only one statement which is the printf() routine. This is a C function that prints its arguments at the terminal or on the video display. This statement prints the statement "Hello World" on to the screen. The characters backslash (\) and the letter n constitute the newline character. This tells the C system to print the statement and proceed to a new line. This is similar to the carriage return key on a typewriter.

Note: All program statements in C must be terminated by a semicolon.

Consider the following program which demonstrates the usage of \n.


/* Program for displaying two strings */

#include <stdio.h>
void main()
  {
    printf("Hello World \n Programming in C is interesting
! \n");
  }

Compilation of a C program

First using a text editor one has to create a file with a .c extension and save the required program. As discussed in the previous chapter, on any system that has C compiler, the simplest command which initiates compilation is:

  cc prg1.c

Here the source program is saved as prg1.c. There are many C compilers around. The cc being the default Sun compiler. The GNU C compiler gcc is available for many platforms. PC users may also be familiar with the Borland bcc compiler. All c compilers operate in the same manner, and share a number of compiler options. Refer the appendix for a list of available compiler options or online help by typing man cc.

Through out this course we will refer to the cc compiler. Other compilers can simply be substituted in place of cc unless otherwise mentioned.

In brief the compilation process consists of the following steps :

Preprocessor - The Preprocessor accepts source code as input and is responsible for interpreting special preprocessor directives denoted by #. Like #include statements and #define statements.

C compiler - The compiler examines the source code for any syntax errors. Any errors discovered will be reported on the screen and the compilation ends. Logical errors are not detected by the compiler. Once the user has fixed the errors and re-compiled the program, the compiler translates the source code to assembly language code.

Assembler - Under UNIX, a seperate assembler processes this assembly code into a binary file. This is then written to a file with a .o extension on a UNIX system or as a .OBJ file on MS-DOS.

Linking - If a source file uses standard library functions or functions defined in other source files the link editor combines these functions with main() to create an executable file. External variable references are resolved here. This process is once again performed automatically when a cc command is issued under UNIX. The executable file in MS-DOS or Windows is the same as the source file name with a .exe extension.

Note : To change the default a.out on UNIX use the following compiler option with the cc command :

  cc -o prg1 prg1.c

This creates an executable by name prg1.exe.

Executing a C program

In order to run or execute a C program just type a.out or if you have renamed the a.out by using the above compiler option then type prg1. This will effect the loading of the program into the computer's memory and initiating its execution. Any runtime errors like division by zero are now encountered. The program needs to be re-analysed for fixing the bugs, and the entire process of compiling, linking and executing should be repeated.

Lint

Before compiling a lengthy C program it is advisable to verify it by using a UNIX utility named "Lint". This can be used as follows :

  lint prg1.c

This utility does not create an executable file, but generates a list of possible bugs in the program. Lint performs type checking of variables and function assignments, checks for efficiency, unused variables and function identifiers, unreachable code and possibly memory leaks. It will also automatically check the program against Standard C Library to ensure that the arguments match in number and in type to any routine used from the library.

Make

Make is one of UNIX tools which simplifies compilation of complex programs or multiple programs. Make first looks for a file called "Makefile", if not found then a file called "makefile". Makefile has a number of components, the details of which are not covered in this chapter. Refer to the appendix for more details on this.

If your C program is a single file, you can usually use make by simply typing

  make prog1

This will compile prog1.c, and put the executable code in prog1.

When a project consists of multiple files, you can compile each C file separately and link them into a program:

  cc -c hello.c
  cc -c goodbye.c
  cc hello.o goodbye.o -o hello

Separate compilation is faster when you're debugging your program, because you only have to recompile the source code files that you modify. When you use make to manage your project, it keeps track of this for you and only recompiles the modules you need.


Listen to audio for Chapter 2

Quiz

Review questions

Assignments

[Your opinion is important to us. If have a comment, correction or question pertaining to this chapter please send it to comments@peoi.org

Previous: History of C programming

Next: Variables, Data Types and Constants