Professional Documents
Culture Documents
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.No
Directive & Description
.
#define
1
Substitutes a preprocessor macro.
#include
2
Inserts a particular header from another file.
#undef
3
Undefines a preprocessor macro.
#ifdef
4
Returns true if this macro is defined.
#ifndef
5
Returns true if this macro is not defined.
#if
6
Tests if a compile time condition is true.
#else
7
The alternative for #if.
#elif
8
#else and #if in one statement.
#endif
9
Ends preprocessor conditional.
#error
10
Prints error message on stderr.
#pragma
11 Issues special commands to the compiler, using a
standardized method.
ANSI C defines a number of macros. Although each one is available for use in
programming, the predefined macros should not be directly modified002E
Sr.No
Macro & Description
.
__DATE__
1 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.
__STDC__
5 Defined as 1 when the compiler complies with the ANSI
standard.
Live Demo
#include <stdio.h>
int main() {
}
Macro in c is defined by the #define directive. Macro is a name given to a piece of
code, so whenever the compiler encounters a macro in a program, it will replace it
with the macro value. In the macro definition, the macros are not terminated by the
semicolon(;).
Syntax of a Macro:
#define macro_name macro_value;
#define pi 3.14;
Example Program:
#include <stdio.h>
int main()
return 0;
From the above code, “a” is a macro name and 10 is the value. Whenever the
compiler encounters a macro name, it will replace it with the macro value.
#include <stdio.h>
// Macro definition
#define LIMIT 5
// Driver Code
" is %d",
LIMIT);
return 0;
File Inclusion in C
#include <filename.h>
Here, filename.h in angle brackets causes the compiler to search for the file in a
series of standard locations specific to implementation. For example, gcc compiler
on Linux, searches for standard library files in a directory called /usr/include.
Other type of #include compiler supports is called local include, whose syntax is as
follows,
#include "filename.h"