A C preprocessor is a statement substitution in C programming language. It instructs the C compiler to do some specific pre-processing before the compilation process. When we compile a C program, C preprocessor processes the statements which are associated with it and expands them for further compilation.
All preprocessor commands begin with a hash symbol (#).
File inclusion preprocessor directive instructs the compiler to include a file in the soure code of a program before executing the source code. C Programming supports two types of files for file inclusion.
i. Standard Library Files
These files are stored in standard header document catalog and contain predefined C programming functions like printf() and scanf().
Example :
#include <filename.h> // Standard Library file
ii. User Defined Files
These are user defined local directory files which a user can include when a function or variable from one file is required to be used in another program file.
Example :
#include "filename.c" // local c file
This preprocessor is the most valuable preprocessor order in C language. We use it to characterize a name for specific worth/consistent/articulation. C preprocessor forms the characterized name and replaces every event of a specific string/characterized name (full scale name) with a given worth (smaller scale body).
#define is used to define a macro.
Example :
#include <stdio.h> // macro definition #define LIMIT 10 int main() { for (int i = 0; i <= LIMIT; i++) { printf("%d \n",i); } return 0; }
Macro with parameter
#include <stdio.h> // macro with parameter #define AREA(b,h) ((b * h)/2) int main() { int b = 10, h = 5, area; area = AREA(b,h); printf("Area of a triangle is: %d", area); return 0; }
Conditional compilation directives help user to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of preprocessing commands like ifdef, ifndef, else, if, elif and endif.
Example :
#include <stdio.h> #define AREA(b,h) ((b * h)/2) int main() { int b = 10, h = 5, area; #ifdef AREA area = AREA(b,h); printf("Area of a triangle is: %d", area); #else #define AREA(b,h) (b*h) area = AREA(b,h); printf("Area of a rectangle is: %d", area); #endif return 0; }
A macro is normally confined to a single line. The macro continuation operator (\) is used if a macro needs to go beyond a single line just like what needs to be done for a string.
A couple of macros are predfined in macros which can directly be used in the source code without defining them.
Predefined Macro | Description |
---|---|
__DATE__ | Contains current date as a character literal in "MMM DD YYYY" format. |
__TIME__ | Contains current time as a character literal in "HH:MM:SS" format. |
__FILE__ | Contains the current filename as a string literal. |
__LINE__ | Contains the current line number as a decimal constant. |
__STDC__ | Defined as 1 when the compiler complies with the ANSI standard. |
Example :
printf("Date :%s\n", __DATE__ );
The stringize operator converts a macro parameter into a string constant. This operator is only used in a macro having a specified argument or parameter list.
Example :
#include <stdio.h> #define message(a,b) \ printf(#a " and " #b ", Welcome to Webtrickshome.\n") int main(void) { message_for(Hello, World); }
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.
Example :
#include <stdio.h> #define token(n) printf ("Token value : %d", token##n) int main(void) { int token40 = 12345; token(40); }
There are some more preprocessing directives which are not used much in general.
The undef directive is used to undefine an existing macro.
Example :
#undef LIMIT
The error directive is used to terminate compilation process of a program if there are conflicting requests.
Example :
#include<stdio.h> #ifndef _MATH_H #error include math.h or provide a declaration of 'sqrt' #else int main() { float x = sqrt(4); printf("%f", x); return 0; } #endif
This directive is used to turn on or off some features. This type of directives are compiler specific i.e. they vary from compiler to compiler.
#pragma startup and #pragma exit
These directives specifies the functions that are needed to run before the control passes to main() function and just before the control returns from main() function.
Example :
#include <stdio.h> void start(); void end(); #pragma startup start #pragma exit end'
This directive is used to hide the warning messages which are displayed during compilation.
#pragma warn -rvl
This directive hides the warnings which are raised when a function supposed to return a value does not return a value.
#pragma warn -par
This directive hides those warnings which are raised when a function does not use the parameters passed to it.
#pragma warn -rch
This directive hides those warnings which are raised when a code written after the return statement in a function is unreachable.
Leave a comment