The Preprocessor
A unique feature of c language is the preprocessor. A program can use the tools provided by preprocessor to make his program easy to read, modify, portable and more efficient.
Preprocessor is a program that processes the code before it passes through the compiler.
It operates under the control of preprocessor command lines and directives. Preprocessor directives are placed in the source program before the main line before the source code passes through the compiler it is examined by the preprocessor for any preprocessor directives. If there is any appropriate actions are taken then the source program is handed over to the compiler.
The preprocessor offers several features called Preprocessor directives .Preprocessor directives follow the special syntax rules and begin with the symbol # and do not require any semicolon at the end. A set of commonly used preprocessor directives
Preprocessor directives:
Directive
Function
#define
Defines a macro substitution
#undef
Undefined a macro
#include
Specifies a file to be included
#ifdef
Tests for macro definition
#endif
Specifies the end of #if
#ifndef
Tests whether the macro is not def
#if
Tests a compile time condition
#else
Specifies alternatives when # if test fails
The preprocessor directives can be divided into three categories
1. Macro substitution /expansion directive
2. File inclusion directive
3. Conditional Compiler directive
Macros:
Macro substitution is a process where an identifier in a program is replaced by a pre defined string composed of one or more tokens we can use the #define statement for the task.
It has the following form
#define identifier string
The preprocessor replaces every occurrence of the identifier the source code by a string. The definition should start with the keyword #define and should follow on identifier and a string with at least one blank space between them. The string may be any text and identifier must be a valid c name.
There are different forms of macro substitution. The most common form is
1.Simplemacrosubstitution
2.Argumentmacrosubstitution
Simple macro substitution:
Simple string replacement is commonly used to define constants example:
#define pi 3.1415926
Writing macro definition in capitals is a convention not a rule a macro definition can include more than a simple constant value it can include expressions as well. Following are valid examples:
#define AREA 12.36
#include
#include
#define kimt 10
void main()
{
int i;
clrscr();
for(i=0;i
printf("%d\n",i);
getch();
}
Macros as arguments:
The preprocessor permits us to define more complex and more useful form of replacements it takes the following form.
# define identifier(f1,f2,f3…..fn) string.
Notice that there is no space between identifier and left parentheses and the identifier f1,f2,f3 …. Fn is analogous to formal arguments in a function definition.
There is a basic difference between simple replacement discussed above and replacement of macro arguments is known as a macro call
A simple example of a macro with arguments is
# define CUBE (x) (x*x*x)
If the following statements appears later in the program,
volume=CUBE(side);
The preprocessor would expand the statement to
volume =(side*side*side)
#include
#include
#define cube(x) (x*x*x)
void main()
{
int i;
clrscr();
i=cube(5);
printf("%d\n",i);
getch();
}
Undefining a macro:
A defined macro can be undefined using the statement
# undef identifier.
This is useful when we want to restrict the definition only to a particular part of the program.
File inclusion:
The preprocessor directive "#include file name” can be used to include any file in to your program if the function s or macro definitions are present in an external file they can be included in your file
In the directive the filename is the name of the file containing the required definitions or functions alternatively the this directive can take the form
#include< filename >
Without double quotation marks. In this format the file will be searched in only standard directories.
The c preprocessor also supports a more general form of test condition #if directive. This takes the following form
#Ifconstantexpression
{
statement-1;
statemet2’
….
….
}
#endif
the constant expression can be a logical expression such as test < = 3 etc
If the result of the constant expression is true then all the statements between the #if and #endif are included for processing otherwise they are skipped. The names TEST LEVEL etc., may be defined as macros.
Conditional Compiler directive
C preprocessor provides a conditional compilation directive which is used to select alternate segment of code in a c program depending upon the condition.
1. #ifdef #else #endif
2. #ifndef #else #endif
3. #if #else #endif
The #ifdef #else #endif directive allows us to skip part of the source code.The genral form of this preprocessor directive is:
#ifdef macroname
statement1;
#else
statement2;
#endif
In case we have used #define for macro_name then statement1 will be compiled otherwise statement2.
#include
#include
#define test
void main()
{
clrscr();
#ifdef test
printf("kimt");
#else
printf("mbd");
#endif
getch();
}
The #ifndef #else #endif directive works exactly opposite to the directive #ifdef #else #endif.The genral form of this preprocessor directive is:
#ifndef macroname
statement1;
#else
statement2;
#endif
In case we have not used #define for macro_name then statement1 will be compiled otherwise statement2.
#include
#include
#define test
void main()
{
clrscr();
#ifdef test
printf("kimt");
#else
printf("mbd");
#endif
getch();
The #if #else #endif directive tests an expression for non zero value and performs the conditional compilation. The genral form of this preprocessor directive is:
#if constant expression
statement1;
#else
statement2;
#endif
In case the constant expression following #if is true (non-zero) then statement1 is compiled otherwise statement2.
#include
#include
#define int test=6
void main()
{
clrscr();
#if test<5
printf("kimt");
#else
printf("mbd");
#endif
getch();
}