Preprocessor in C

Advertisements

Prev Tutorial Next Tutorial

Pre-processor in C

Preprocessor is a program which will executed automatically before passing the source program to compiler. This process is called pre-processing. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.

preprocessor in c

The C Preprocessor is not a part of compiler, it is a separate program invoked by the compiler as the first part of translation.

Commands used in preprocessor are called preprocessor directives and they begin with pond "#" symbol and should be not ended with (;).

Defined Proprocessor Directive

Proprocessor Directive can be place any where in the program, but generally it place top of the program before defining the first function.

Example

#include<stdio.h>
#define PI 3.14
void main()
{
printf("%f",PI);
}

Output

3.14

C language preprocessor directives

  • Macro substitution directives. example: #define
  • File inclusion directives. example: #include
  • Conditional compilation directive. example: #if, #else, #ifdef, #undef
  • Miscellaneous directive. example: #error, #line
PreprocessorSyntaxDescription
Macro#defineThis macro defines constant value and can be any of the basic data types.
Header file inclusion#include <file_name>The source code of the file "file_name" is included in the main program at the specified place
Conditional compilation#ifdef, #endif, #if, #else, #ifndefSet of commands are included or excluded in source program before compilation with respect to the condition
Other directives#undef, #pragma#undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program

#ifdef, #else and #endif

"#ifdef" directive checks whether particular macro is defined or not. If it is defined, "If" clause statements are included in source file. Otherwise, "else" clause statements are included in source file for compilation and execution.

Example

#include<stdio.h>
#include<conio.h>

#define AGE 18

void main()
{
clrscr();
#ifdef AGE
{
printf("Eligible for voting\n");
}
#else
{
printf("Not eligible\n");
}
#endif
getch();
}

Output

Eligible for voting

#if, #else and #endif

"If" clause statement is included in source file if given condition is true. Otherwise, else clause statement is included in source file for compilation and execution.

Example

#include<stdio.h>
#include<conio.h>

#define AGE 18

void main()
{
clrscr();
#if (AGE>=18)
{
printf("Eligible for voting");
}
#else
{
printf("\nNot eligible");
}
#endif
getch();
}

Output

Eliginle for voting

undef

This directive undefines existing macro in the program. In below program we first undefine age variable and again it define with new value.

Example

#include<stdio.h>
#include<conio.h>

#define age 18
void main()
{
clrscr();
printf("First defined value for age: %d\n",age);
#undef age      // undefining variable
#define age 30  // redefining age value for for new value
printf("Age after undef redefine: %d",age);
getch();
}

Output

First defined value for age: 18
Age after undef redefine: 30

pragma

Pragma is used to call a function before and after main function in a C program.

Example

#include<stdio.h>
#include<conio.h>
#include<dos.h>

void function1( );
void function2( );

#pragma startup function1
#pragma exit function2

void main( )
{
delay(1000);
printf ("\nI am main function" ) ;
}
void function1( )
{
clrscr();
delay(500);
printf("\nI am function1");
}

void function2( )
{
delay(1000);
printf ( "\nI am function2" );
getch();
}

Output

I am function1
I am main function
I am function2

Explanation: Here delay() function are used for give delay or wait time for execution of code, this function is present in dos.h header file. With the help of delay function you can see clearly flow of above program.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements