The Preprocessor

A unique feature of C language is the preprocessor. A program can use the tools provided by the preprocessor to make the program easy to read, modify, portable and more efficient.
  • Preprocessor is a program that processes the code before it passes through the compiler. It operates under the control of preprocessor command line and directives. Preprocessor directives are placed in the source program before the main line, before the source code passes through the compiler it is examined by the preprocessor for any preprocessor directives.
  • Preprocessor directives follow the special syntax rules and begin with the symbol '#' in column1 (starting of the program) and do not require any semicolon at the end. A set of commonly used preprocessor directives are as follows.
    • #define : defines a macro substitution
    • #include : specifies a file to be included

The preprocessor directives can be divided into three categories.
  1. Macro substitution division
  2. File inclusion division
  3. Compiler control division

Macros

Macro substitution is a process where an identifier in a program is replaced by a pre-defined string composed of one or more tokens we can use the #define statement for the task.

Define identifier string

The preprocessor replaces every occurrence of the identifier in the source code by a string. The definition should start with a keyword #define and should follow on identifier and a string with at least one blank space between them. The string may be any text or number and identifier must be a valid C name.

There are different forms of macro substitution. The most common form are as follows.
  • Simple macro substitution
  • Argument macro substitution
  • Nested macro substitution
The preprocessor directive #include <filename.h> can be used to include any file into the program if the functions or macro definitions are present in an external file they can be included in file. In the directive the filename is the name of the file containing the required definitions or functions alternatively this directive can take the form #include<filename.h> without double quotation marks.

In this format the file will be searched in only standard directories. The C preprocessor also supports a more general form of test condition #if directive. This takes the following form.
#if constant expression
{
statement 1;
statement 2;
...........
...........
statement n;
}
#endif

The constant expression can be a logical expression such as test <= 3 etc.

C Header Files

In this paragraph, we explore the use of header files in C, ways in which they can be used, and the philosophy for breaking up code into separate source files.

C header files are used to provide a way of telling the compiler what interface specific functions require in order to be used. The compiler has no interest in what the source code actually does, but it does not need to know how interface to that code, and the header file give it that information.

Including a header file produces the same results as copying the header file into the each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. In C, the usual convention is to give header files names that ends with '.h'. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot.

Standard Header Files

By now, the reader should be aware that there are many operations that are not catered for the in the C programming language, string manipulation, for example. However, the creators of all compiler sets will make available a set of libraries to provide this 'missing' functionality.

These are collectively known as the standard libraries and include:
  • string.h : for string handling 
  • stdlib.h : for some miscellaneous functions
  • stdio.h : standardized input and output
  • math.h : mathematical functions 
We place references to these at the start of the source code file that uses one or more functions from that library. So, a simple hello world application might require the formatted output offered in the stdio.h header file.
#include <stdio.h>
#include <conio.h>

void main(void)
{
printf ("Hello World");
getch();
}

This program simply writes Hello World  to the output screen. What is important to note is that the code for the function printf() is not defined in the program itself, but in a library that comes pre-compiled. The compiler can compile the source code above, thanks to the #include line, which tells it where the definition of printf() can be found.