Macros and Preprocessors in C

C++Server Side ProgrammingProgramming

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 −

Sr.NoDirectives & Descriptions
1#define
Substitutes a preprocessor macro.
2#include
Inserts a particular header from another file.
3#undef
Undefines a preprocessor macro.
4#ifdef
Returns true if this macro is defined.
5#ifndef
Returns true if this macro is not defined.
6#if
Tests if a compile time condition is true.
7#else
The alternative for #if.
8#elif
#else and #if in one statement.
9#endif
Ends preprocessor conditional.
10#error
Prints error message on stderr.
11#pragma
Issues special commands to the compiler, using a standardized method.

Some examples of Preprocessors −

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"

These directives tell the CPP to get stdio.h from System Libraries and add the text to the current source file. The next line tells CPP to get myheader.h from the local directory and add the content to the current source file.

#undef FILE_SIZE
#define FILE_SIZE 42

It tells the CPP to undefine existing FILE_SIZE and define it as 42.

#ifndef MESSAGE
#define MESSAGE "You wish!"
#endif

It tells the CPP to define MESSAGE only if MESSAGE isn't already defined.

#ifdef DEBUG
/* Your debugging statements here */
#endif

It tells the CPP to process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to the gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.

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.

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

Example

#include <stdio.h>
int main() {
   printf("File :%s\n", __FILE__ );
   printf("Date :%s\n", __DATE__ );
   printf("Time :%s\n", __TIME__ );
   printf("Line :%d\n", __LINE__ );
   printf("ANSI :%d\n", __STDC__ );
}

Output

File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1
raja
Published on 26-Apr-2019 11:54:30
Advertisements