C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. The C Preprocessor is just a text substitution tool. It is a micro processor. 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 is knows as C preprocessor.
A list of preprocessor directives.
Directive | Description |
#define | Substitutes a preprocessor macro. |
#include | Inserts a particular header from another file. |
#undef | Undefines a preprocessor macro |
#ifdef | Returns true if this macro is defined. |
#ifndef | Returns true if this macro is not defined. |
#if | Tests if a compile time condition is true. |
#else | The alternative for #if. |
#elif | #else and #if in one statement. |
#endif | Ends preprocessor conditional. |
#error | Prints error message on stderr. |
#pragma | ssues special commands to the compiler, using a standardized method. |
Macros
A segment of code which is replace by the value of macro is knows as Macros. Macro is define by #define directive.
There are two type of Macros
- Object-like Macros
- The object-like macro is an identifier that is replace by value. It is widely use to represent numeric constants is knows as Object like Macros.
- Function-like Macros
- The function-like macro looks Just like function call is knows as Function Like Macros.
Predefined Macros in C Programming Language.
ANSI C defines a number of macros. T he predefined macros should not be directly modified.
Macro | Description |
_DATE_ | represents current date in “MMM DD YYYY” format. |
_TIME_ | represents current time in “HH:MM:SS” format. |
_FILE_ | represents current file name. |
_LINE_ | represents current line number. |
_STDC_ | Defined as 1 when the compiler complies with the ANSI standard. |
Example
Input value
#include <stdio.h> int main() { printf("Date :%s\n", __DATE__ ); printf("Time :%s\n", __TIME__ ); printf("ANSI :%d\n", __STDC__ ); printf("File :%s\n", __FILE__ ); printf("Line :%d\n", __LINE__ ); }
Output value
#include
The #include preprocessor directive is use to paste code of given file into current file. It is use include system define and user define header files is knows as #include.
Example
Input value
#include<stdio.h> int main(){ printf("basic Engineer"); return 0; }
Output value
#define
The #define preprocessor directive is use to define constant substitution is knows as #define.
Syntax
- #define token value
Example
Input value
#include <stdio.h> #define e 2.71828 main() { printf("%f",e); }
Output value
#underf
The #undef preprocessor directive is use to undefine the constant define by #define is knows as #underf.
Example
Input value
#include <stdio.h> #define number 12 int square=number*number; #undef number main() { printf("%d",square); }
Output value
#ifdef
The #ifdef preprocessor directive checks if macro is define by #define. If yes, it executes the code otherwise #else code is execute is knows as #ifdef.
Syntax
- #ifdef MACRO
- //code
- #endif
Example
Input value
#include <stdio.h> #include <conio.h> #define NOINPUT void main() { int s=0; #ifdef NOINPUT s=45; #else printf("Enter s:"); scanf("%d", &s); #endif printf("Value of s: %d\n", s); getch(); }
Output value
#ifndef
The #ifndef preprocessor directive checks if macro is not define by #define. If yes, it executes the code otherwise #else code is execute is knows as #ifndef.
Syntax
- #ifndef MACRO
- //code
- #endif
Example
Input value
#include <stdio.h> #include <conio.h> void main() { int x=0; #ifndef INPUT x=45; #else printf("Enter x:"); scanf("%d", &x); #endif printf("Value of a: %d\n", x); getch(); }
Output value
#pragma
The #pragma preprocessor directive is used to provide additional information to the compiler is know as #pragma
Syntax
#pragma token
Example
Input value
#include<stdio.h> #include<conio.h> void func() ; #pragma startup func #pragma exit func void main(){ printf("\nbasic engineer"); getch(); } void func(){ printf("\nbasic engineer"); getch(); }
Output value
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about C Programming language please Wikipedia click here .
Stay Connected Stay Safe, Thank you
0 Comments