C Preprocessors

Preprocessor Directives extends the power of C programming language.C Preprocessors Directives is a special set of instruction in program which is processed before handing over the program to the compiler. These instructions are always preceded by a pound sign (#) and NOT terminated by a semicolon. The preprocessor directives are executed before the actual compilation of code begins.Different preprocessor directives (commands) tell the preprocessor to perform different actions. We can categorize the Preprocessor Directives as follows:

  • File inclusion directives
  • Macro substitution directives
  • Conditional compilation directives

Advantages:

  • It make program development easy
  • It enhance the readability of the program
  • It make modification easy
  • It increase the transportability of the program between different machine architectures.

File inclusion directives

The file inclusion directive is used to include files into the current file. When the preprocessor encounters an #include directive it replaces it by the entire content of the specified file.The file inclusion directive (#include) can be used in following two ways:

  • #include “file-name”
  • #include <file-name>

We can see that #include can be used in two ways angular brackets (<>)and inverted commas (“”). The only difference between both expressions is the location (directories) where the compiler is going to look for the file. In the first case where the file name is specified between double-quotes, the compiler will look for the in the current directory that includes the file containing the directive. In case if it is not there the compiler will look the file in the default include directories where it is configured to look for the standard header files.When #include is written with <> it means the file is searched directly where the compiler is configured to look for the standard header files. Therefore, standard header files are usually included in angle-brackets, while other specific header files are included using quotes.

Take a look at below example program. Save the following code in a file called main.c:

Then save the code below in a file called head.h and place it in the same directory as main.c:

The variable declared in the head.h file will be available in main.c, now we can use the variable in main().

 

Macro substitution directives

Macro substitution directives are used to define identifier which is being replaced by a pre defined string in program.Macro substitution is process in which preprocessor replaces identifiers by one, or more program statements (like functions) and they are expanded inline.

There are two directives for Macro Definition:

  • #define – Used to define a macro
  • #undef – Used to undefine a macro

#define preprocessor

To define preprocessor macros we can use #define.

Syntax:

Example:

The token string in above line 3.14 is replaced in every occurance of symbolic constant PI.

C Program to find area of a cricle.

Output:

Conditional compilation directives(#ifdef, #ifndef, #if, #endif, #else and #elif)

Conditional Compilation Directives allows you to include or exclude certain part of code only when certain condition met.These macros are evaluated on compile time.

The following directives are included in this category:

  • #if
  • #elif
  • #endif
  • #ifdef
  • #ifndef

Line control (#line) directive:

The #line directive give us a way to control or set the value of the __LINE__ and __FILE__ macros. (Note: the filename is optional.) The __LINE__ and __FILE__ macros represent the current line being read and the current file being read.

Syntax:

Error directive (#error)

Error directive allows to aborts the compilation process when it is found, ant report a fatal error that can be specified as its parameter.

Pragma directive (#pragma)

The #pragma directive is compiler specific directives and used for the suppression of specific error messages.

Predefined Macros
__LINE__

It represents the line number of the current source file being compiled.

__FILE__

It represents the name of the source file being compiled.

__DATE__

It returns the date, in the form “Mmm dd yyyy”, when the compilation process began.

__TIME__

A string literal in the form “hh:mm:ss” containing the time at which the compilation process began.

__STDC__

Forces the use of standard C/CPP codes only in the program.