Preprocessor Commands
In 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. All preprocessor commands begin with a hash symbol (#).
list of preprocessor directives.
#include
#define
#undef
#ifdef
#ifndef
#if
#else
#elif
#endif
#error
#pragma
C Macros
A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two types of macros:
1. Object-like Macros
2. Function-like Macros
Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants.
For example:
#define PI 3.14
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros
The function-like macro looks like function call.
For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
C #include
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error. By the use of #include directive, we provide information to the preprocessor where to look for the header files. There are two variants to use #include directive.
1. #include
2. #include "filename"
The #include 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.
C #define
The #define preprocessor directive is used to define constant or micro substitution. It can use any basic data type.
Syntax:
#define token value
C #undef
The #undef preprocessor directive is used to undefine the constant or macro defined by #define.
Syntax: #undef token
C #ifdef
The #ifdef preprocessor directive checks if macro is defined by #define.
If yes, it executes the code otherwise #else code is executed, if present.
Syntax:
#ifdef MACRO
#endif
Syntax with #else:
#ifdef MACRO
#else
#endif
C #ifndef
The #ifndef preprocessor directive checks if macro is not defined by #define.
If yes, it executes the code otherwise #else code is executed, if present.
Syntax: #ifndef MACRO
#endif Syntax with #else:
#ifndef MACRO
#else
#endif
C #if
The #if preprocessor directive evaluates the expression or condition.
If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.
Syntax:
#if expression
#endif
Syntax with #else:
#if expression
#else
#endif
C #else
The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives.
Syntax:
#if expression
#else
#endif
C #error
The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found and skips further compilation process.
C #pragma
The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating-system feature.
Syntax:
#pragma token
Different compilers can provide different usage of #pragma directive.