C Preprocessor by G Krishna Chauhan

The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We'll refer to the C Preprocessor as CPP.

All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives −

Directive Description
#define :Substitutes a preprocessor macro.
#include :Inserts a particular header from another file.
#undef :Undefines a preprocessor macro.
#ifdef :Returns true if this macro is defined.
#ifndef :Returns true if this macro is not defined.
#if         :Tests if a compile time condition is true.
#else :The alternative for #if.
#elif         :#else and #if in one statement.
#endif :Ends preprocessor conditional.
#error :Prints error message on stderr.
#pragma :Issues special commands to the compiler, using a standardized method.

Preprocessors Examples:

Analyze the following examples to understand various directives.

#define MAX_ARRAY_LENGTH 20

This directive tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.

#include <stdio.h>
#include "myheader.h"


Predefined Macros:

ANSI C defines a number of macros. Although each one is available for use in programming, the predefined macros should not be directly modified.

Macro and its Description:

__DATE__ The current date as a character literal in "MMM DD YYYY" format.
__TIME__ The current time as a character literal in "HH:MM:SS" format.
__FILE__ This contains the current filename as a string literal.
__LINE__ This contains the current line number as a decimal constant.
__STDC__ Defined as 1 when the compiler complies with the ANSI standard.





Click here for Simple C programs 

No comments:

Post a Comment