C Preprocessor Directives
C Preprocessor Directives
The C preprocessor modifies a source file before handing it over to the compiler,
allowing conditional compilation with #ifdef, defining constants with #define,
including header files with #include, and using builtin macros such as __FILE__. This
page lists the preprocessor directives, or commands to the preprocessor, that are
available:
• #include
• #define
• #undef
• #if
• #ifdef
• #ifndef
• #error
• __FILE__
• __LINE__
• __DATE__
• __TIME__
• __TIMESTAMP__
• pragma
• # macro operator
• ## macro operator
#include
#include <header name>
The include directive instructs the pre-processor to paste the text of the given file
into the current file. Generally, it is necessary to tell the preprocessor where to look
for header files if they are not placed in the current directory or a standard system
directory. This can be done either at compile time or as part of your compiler's
project file. This feature is implementation-specific, so see the compilers page for
more information.
1. #include <filename>
2. #include "filename"
The #include <filename> tells the compiler to look for the directory where system
header files are held. In UNIX, it is \usr\include directory.
The #include "filename" tells the compiler to look in the current directory from
where program is running.
#define
The #define directive takes two forms: defining a constant and creating a macro.
Defining a constant
#define token [value]
Example:
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
}
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main() {
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}
#undef
The #undef directive undefines a constant or preprocessor macro defined previously
using #define.
#undef token
For example:
#define E 2.71828
int e_squared = E * E;
#undef E
Usually, #undef is used to scope a preprocessor constant into a very limited region--
this is done to avoid leaking the constant (for example, changing a single letter like
E would be dangerous across a large program, but in a short scope, it is
comparatively safe. #undef is the only way to create this scope since the
preprocessor does not understand block scopes defined with { and }.
#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}
#if
#if <value>
/* code to execute if this value is true */
#elsif lt;value>
/* code to execute if this value is true */
#else
/* code to execut otherwise
#endif
#if checks whether the value is true (in the C and C++ sense of everything but 0)
and if so, includes the code until the closing #endif. If not, that code is removed
from the copy of the file given to the compiler prior to compilation (but it has no
effect on the original source code file). There may be nested #if statements. It is
common to comment out a block of code using the following construction because
you cannot nest multi-line comments in C or C++.
#if 0
/* code */
#endif
#include <stdio.h>
#define NUMBER 1
void main() {
#if (NUMBER==0)
printf("1 Value of Number is: %d",NUMBER);
#endif
#if (NUMBER==1)
printf("2 Value of Number is: %d",NUMBER);
#endif
}
#ifdef
#ifdef <token>
/* code */
#else
/* code to include if the token is not defined */
#endif
ifdef checks whether the given token has been #defined earlier in the file or in an
included file. If so, it includes everything between it and the closing #else or, if no
#else is present, the closing #endif. #ifdef can be used with built-in token identifiers
set by the compiler to indicate that additional functionality is available. For instance,
the __cplusplus macro is defined in C++ but not C; you can use this fact to mix C
and C++ code using an #ifdef statement:
#ifdef __cplusplus
// C++ code
#else
// C code
#endif
#include <stdio.h>
#define NOINPUT
void main() {
int a=0; #ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
}
#ifndef
#ifndef <token>
/* code */
#else
/* code to include if the token is defined */
#endif
#ifndef checks whether the given token has been #defined earlier in the file or in an
included file; if not, it includes the code between it and the closing #else or, if no
#else is present, #endif statement. #ifndef is often used to make header
files idempotent by defining a token once the file has been included and checking
that the token was not set at the top of that file.
#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif
#error
#error "This code should not compile"
The #error macro allows you to make compilation fail and issue a statement that will
appear in the list of compilation errors. It is most useful when combined
with #if/#elif/#else to fail compilation if some condition is not true. For example:
#ifndef __unix__ // __unix__ is typically defined when targetting Unix
#error "Only Unix is supported"
#endif
1. Macro Expansion
We can also pass arguments to macros; it can be described with arguments that
perform similarly as functions.
Syntax:
Where,
• To modify program: We can change only the macro and it can reflect on the program.
Hence we do not need to change it every time.
#define PrintLOWER 50
void main()
int j;
{
printf("\n%d", j);
void main()
float r = 3.5, x;
x = AREA (r);
• ___TIME___ defines the current time using a character literal in “HH:MM: SS” format.
• ___STDC___ specifies as 1 when the compiler complies with the ANSI standard.
• ___DATE___ shows the present date with a character literal in the “MMM DD YYYY”
format.
Example:
#include<stdio.h>
int main(){
return 0;