Sunday, 13 October 2013

How to use Preprocessor in C


Before you start: To learn how to compile a c program in C click here or click here

Preprocessor is a program to processes the input data and the produce output. In C preprocessor process is a source code before passes to the compiler . Various types of preprocessor are

#define, #ifdef , #endif , #undef , #else , #progma , #error , #line

the are various type of macro i.e  # defined keyword 


                           Compilation Stage :

1st is  Hand written Program
2nd is  C source code (xyz.c)
3rd is  Expanding Source code  (xyz . i)
4th is Object code (xyz.obj)
5th is Executable Code (xyz.exe)

Directives :

Preprocessor is several directives in C .It is a another set of Language in C.
1 Macro definition
2 File inclusion
3 Conditional Compilation
4 Miscellaneous directive


Macro Defination :

#define leng 50
the leng is the macro .Before the compilation process of source code ,the code is passes through  preprocessor which is check for macro. Macro expression replace a macro with specific chank of code
#define LENG 50 
int main()
 int  t = 10;
printf  ("\n Sum " , t + LENG );
getch ();
return 0;
output :
sum 60

File Inclusion :

source code divide into multiple files and each file are contain multiple function that are interrelated to each other .In addition of multiple utility function and macros are used in multiple file i.e such function and macros are common use we can write .this concept of inclusion ,which is allows entire content of file to insert some source code.
#include "file" .h stands for header file
#include "mycode.c"

conditional compilation :

conditional  compilation are  #ifdef , #endif , #undef , #else , #ifndef
#ifdef macro
  statement 1;
  statement 2;
 statement n;

Miscellaneous Directive :

They are used for different purpose
#undef , #pragma ,#error ,#line

To learn more in C Click here