18. Processors-in-C
18. Processors-in-C
Topperworld.in
C Preprocessors
©Topperworld
C Programming
#elif
#endif
#error
#pragma
1. Macros
A macro is a segment of code which is replaced by the value of macro. Macro
is defined by #define directive.
Macros are pieces of code in a program that is given some name. Whenever
this name is encountered by the compiler, the compiler replaces the name
with the actual piece of code.
The ‘#define’ directive is used to define a macroThere 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.
Function-like Macros
©Topperworld
C Programming
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
Example:
#include <stdio.h>
int main() {
When the above code in a file test.c is compiled and executed, it produces the following
result −
Output:
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1
©Topperworld
C Programming
2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in
the source code program.
The #include preprocessor directive is used to include the header files in
the C Program.
3. Conditional Compilation
Conditional Complation is a type of directive that helps to compile a
specific portion of the program or to skip the compilation of some specific
part of the program based on some conditions.
There are the following preprocessor directives that are used to insert
conditional code:
#if Directive
#ifdef Directive
#ifndef Directive
#else Directive
#elif Directive
#endif Directive
4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
#undef Directive
#pragma Directive
1. #undef Directive
2. #pragma Directive
This directive is a special purpose directive and is used to turn on or off some
features. These types of directives are compiler-specific, i.e., they vary from
compiler to compiler.
©Topperworld