Saturday 2 August 2014

Preprocessor in C

Preprocessor 

A preprocessor is a program which will executed automatically before passing the source program to compiler means it takes the source c program as an input before compilation and reads all preprocessor statements begin with #(hash) symbol and processes those statements which may result in the alteration of the 'C' code and generates a complete 'C' file and gives it to the compiler as an input.All preprocessor directives should be not ended with (;).

Preprocessor statements are also called the preprocessor directive.We can place pre-processor directives anywhere in the program, but generally recommended to place top of the program before defining the first function.

These statements instruct the compiler to include C preprocessors such as header files and symbolic constants before compiling the C program. Some of the preprocessor statements are listed below.


Header Files

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

Symbolic Constants


#define PI 3.14159
#define TRUE 1
#define FALSE 0


In ‘C’ language pre-processor directives are classified into 4 types

  1. Macro substitution directives. example: #define
  2. File inclusion directives. example: #include
  3. Conditional compilation directive. example: #if, #else, #ifdef, #undef
  4. Miscellaneous directive. example: #error, #line

#if, #elif, #else, #endif

These preprocessing directives create conditional compiling parameters that control the compiling of the source code. They must begin on a separate line.

Syntax:
#if constant_expression
#else
#endif

or

#if
 constant_expression
#elif constant_expression
#endif

The compiler only compiles the code after the #if expression if the constant_expression evaluates to a non-zero value (true). If the value is 0 (false), then the compiler skips the lines until the next #else#elif, or#endif. If there is a matching #else, and the constant_expression evaluated to 0 (false), then the lines between the #else and the #endif are compiled. If there is a matching #elif, and the preceding #if evaluated to false, then the constant_expression after that is evaluated and the code between the #elif and the #endif is compiled only if this expression evaluates to a non-zero value (true). 

No comments:

Post a Comment