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
#defineSubstitutes a preprocessor macro.
#includeInserts a particular header from another file.
#undefUndefines a preprocessor macro
#ifdefReturns true if this macro is defined.
#ifndefReturns true if this macro is not defined.
#ifTests if a compile time condition is true.
#elseThe alternative for #if.
#elif#else and #if in one statement.
#endifEnds preprocessor conditional.
#errorPrints error message on stderr.
#pragmassues 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.

MacroDescription
_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

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

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

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

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

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

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

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


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *