C Preprocessors

In this tutorial you will learn about the C Preprocessors and its application with practical example.

Preprocessor Directives extend the power of the C programming language. C Preprocessors Directives is a special set of instructions in the 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 makes program development easy
  • It enhances the readability of the program
  • It makes modification easy
  • It increases 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 c preprocessors encounters an #include directive it replaces it with the entire content of the specified file. The file inclusion directive (#include) can be used in the 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 it is not there the compiler will look at the file in the default including 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 the below example program. Save the following code in a file called main.c:

Then save the code below in a file called head. hand places it in the same directory as the main.c:

The variable is 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 an identifier that is being replaced by a pre-defined string in the program. Macro substitution is a process in which the preprocessor replaces identifiers with 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 c preprocessors

To define preprocessor macros we can use #define.

Syntax:

Example:

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

C Program to find the area of a circle.

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 are 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 gives 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 reports a fatal error that can be specified as its parameter.

Pragma directive (#pragma)

The #pragma directive is a compiler-specific directive 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.

 

In this tutorial we have learn about the C Preprocessors and its application with practical example. I hope you will like this tutorial.