preprocessor directive

Preprocessor Directive in c

The preprocessor scans and modifies the source code before the compilation. The main function performed by the preprocessor are

1. Simple macro substitution.
2. Conditional compilation
3. Macro with parameters
4. File inclusion
5. Error generation and pragma directives.

Advantage of using Preprocessor

1. program modification easy
2. program become portable
3. used for testing and debugging
4. Enhances readability of the program

Any start line start with # symbol known as preprocessor directives. When the preprocessor find the line start with a # symbol, it consider that the line is a command for itself and works according So all the directives executed by the preprocessor , and compiler does not read any line starting with # symbol.

List of all preprocessor Directives

#define
#include
#else
#if
#elif
#endif
#error
#ifdef
#ifndef
#undef
#line
#pragma

c define

The general syntax is:

#define macro_name macro_expression

The macro_name is any valid C identifier. and it is write generally in capital letters to distinguish it from others variable The macro_expansion can be any text. There should be space necessary between macro name and macro expansion. The C preprocessor replaces all the occurrences of the micro_name with the macro_expansion let us take some examples of #define directive

#define FALSE 0
#define TRUE 1
#define PI 3.14556

#include

The processor directive #include is used to include a file into the source code of program. we used this in the program to include header files. The file name should be between angle brackets. The syntax is

#include< filename >
#include”filename”

The after including the file by #include the complete content of the file can be used in the program. when the filename is with in the angle brackets, then the file is searched in the standard include directory only

#else and #elif in c

#else is used with the #if preprocessor directive. It is like if-else control structure. The syntax is as-

#if constant-expression
statements
……….

#else
statements
……….

#endif

if the constant expression is true(non zero) then the statements between #if and #else are compiled otherwise the statements between #else and #endlif are compiled lets take the example to understand #else directive

/* program to understand the use of #else directive */
#include<stdio.h>
#define
COUNT 10
int main()
{
int a=15,b=6;
#if COUNT>8
printf("COUNT is gratter then 10\n");
a=a-b;
b=a+b;
#else
printf("Value of COUNT value is less then 8 \n ");
a=a*b;
#endif
printf("a=%d and b=%d \n",a,b);
return 0;
}