C - Preprocessors
C - Preprocessors
C - Preprocessors
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 −
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 1/7
2/15/2020 C - Preprocessors - Tutorialspoint
1
#define
2
#include
Inserts a particular header from another file.
3
#undef
4
#ifdef
Returns true if this macro is defined.
5
#ifndef
6
#if
7
#else
8
#elif
9
#endif
Ends preprocessor conditional.
10
#error
11
#pragma
Preprocessors Examples
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 2/7
2/15/2020 C - Preprocessors - Tutorialspoint
#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
#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.
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 3/7
2/15/2020 C - Preprocessors - Tutorialspoint
1
__DATE__
The current date as a character literal in "MMM DD YYYY" format.
2
__TIME__
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__
Live Demo
#include <stdio.h>
int main() {
When the above code in a file test.c is compiled and executed, it produces the following result −
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1
Preprocessor Operators
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 4/7
2/15/2020 C - Preprocessors - Tutorialspoint
A macro is normally confined to a single line. The macro continuation operator (\) is used to continue a
macro that is too long for a single line. For example −
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
The stringize or number-sign operator ( '#' ), when used within a macro definition, converts a macro
parameter into a string constant. This operator may be used only in a macro having a specified
argument or parameter list. For example −
Live Demo
#include <stdio.h>
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
int main(void) {
message_for(Carole, Debra);
return 0;
}
When the above code is compiled and executed, it produces the following result −
The token-pasting operator (##) within a macro definition combines two arguments. It permits two
separate tokens in the macro definition to be joined into a single token. For example −
Live Demo
#include <stdio.h>
int main(void) {
int token34 = 40;
tokenpaster(34);
return 0;
}
When the above code is compiled and executed, it produces the following result −
token34 = 40
It happened so because this example results in the following actual output from the preprocessor −
This example shows the concatenation of token##n into token34 and here we have used both
stringize and token-pasting.
Live Demo
#include <stdio.h>
int main(void) {
printf("Here is the message: %s\n", MESSAGE);
return 0;
}
When the above code is compiled and executed, it produces the following result −
Parameterized Macros
One of the powerful functions of the CPP is the ability to simulate functions using parameterized
macros. For example, we might have some code to square a number as follows −
int square(int x) {
return x * x;
}
Macros with arguments must be defined using the #define directive before they can be used. The
argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are
not allowed between the macro name and open parenthesis. For example −
Live Demo
#include <stdio.h>
int main(void) {
printf("Max between 20 and 10 is %d\n", MAX(10, 20));
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 6/7
2/15/2020 C - Preprocessors - Tutorialspoint
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm 7/7