FEATURES OF C PREPROCESSOR

In preprocessor, it offer several features called preprocessor directive.

1.macro expansion

2.file inclusion

3.conditional compilation

4.misclanious directives

 Macro expansion

The preprocessor allows you to define macros using the #define directive. Macros are essantily textual substitution that are performed by the preprocessor before the actual compilation . they can be used to define constants , inline function, or to perform code generation.

#include<stdio.h>

# define UPPER 25

int main(){

  int i;

  for(i=1; i<=UPPER; i++){

  printf(“%d”, i);

  return 0;

}

}

File inclusion

You can use the #include directive to include the content of another file at the point where the directive appears. This allow you to organize your code into multiple files and reuse common code accross different source file.

#include “file name”

#include <library name>

Conditional compilation

The preprocessor provide conditional compilation directives such as #if , #ifdef , #ifndef , #else , and #endif. these directive allow you to include or execute specific section of code based on condition that’s are evaluated during preprocessing. This feature is commonly used for platform specific code or to enable/disable certain feature at that compile time.

// structure of conditional compilation

#ifdef macro name

Statement 1;

Statement 2;

Statement 3;

#endif

Ex

#include<stdio.h>

#define INTEL 1

int main(){

  #ifdef INTEL

  printf(“code suitable for intel /n”);

  #else

  printf(“code suitable for AMD /n”);

  #endif

  printf(“common code”);

  return 0;

}

Misclanious directive

There are two more preprocessor directive

1.#undef

2.#porgma

#undef directive -> on some actions if we want to undef a variable we can use #undef directive.

Ex

#include<stdio.h>

#define INTEL 1

#undef INTEL

void main()

{

  #indef INTEL

  printf(“code suitable for intel /n”);

  #else

  printf(“code suitable for AMD /n”);

  #endif

  printf(“common code”);

}

 Output: code suitable for AMD common code

 

#paragma -> the preprocessor may include additional directive specific to the compiler being used. These directive obtain starts with #paragma and provide compiler specific instructions or optimization.

 

There are two keywords startup and exit which run before and after main method.

 

#paragma has some keyword related to windows so they will run only on windows compiler.

#include<stdio.h>

void fun1();

void fun2();

  #paragma startup fun1;

  #paragma exit fun2;

void main(){

  printf(“in main /n”);

}

void fun1(){

  printf(“in fun1 /n”);

}

void fun2(){

  printf(“in fun2 /n”);

}

 

 

Processor

Input

Output

Editor

Program typed from keyword

C source code containing program and preprocessor commands.

Preprocessor

C source code file

Expended source code file created after processing preprocessor commands.

Compiler

Expended source code file

Assembly language code

Assemble

Assembly language code

Relocatable object code in machine language.

Linker

Object code of our program and object code of library function

Executable code in machine language.

Loader

Executable file