stu nicholls dot com | menu - Professional dropdown #3

Untitled Document

C- Preprocessors

This is a program that processes our source program before it is passed to the compiler. Preprocessor commands (generally known as directives) make a program easy to read, modify, and more efficient

Features of C preprocessors:

The preprocessor commands often known as directives begin with # symbol.
These directives can be placed anywhere in a program but most oftenly placed at the beginning of the program i.e., before main () or before the beginning of a particular function.
In C we have following preprocessor directives:

  1. Macro expansion directives
  2. File inclusion directives
  3. Compiler control directives
Examples for Preprocessor Directives are # include, # define, # else, # endif, # if, # elif

When a program is compiled, before the source code is passed through the compiler, it is examined by preprocessor for any macro-definition.
When it encounters # (hash) sign and define directive it goes through the entire program to search for macro template and replaces it with the appropriate macro expansion.
Only after this procedure has been completed, the program is handed over to compiler.

# include <stdio.h>
# define a 25
main ()
int b;
printf ("a is %d", a);
b = a + 25;
printf ("b is %d\n", b);

In the example the value of a is replaced by 25 by the preprocessor in the source code (program) before it is passed on to the compiler.
Rules to use Macros:
  1. It is only customary to use capital letters for macro template.
  2. Macro template and its macro expansion is separated by blank space or tabs.
  3. The blank spaces between the # sign and define statement is optional.
  4. Macro definition is never terminated by a semicolon.

Use of Macro - definitions:
  1. To improve the readability.
  2. If the value of say a is changed (see the above program) and if macro is not used then, we have to go to every place and change the values manually wherever it is used.
  3. If the macros are used, you can change the values only at macro definition.
Although we can use the variables for the same purpose we won't use it because:
  1. It is inefficient since the compiler can generate faster and micro compact code for constants than it can for variables.
  2. In the program if the variable changes because of something somewhere then the concept of constants will not hold good.

Define directive and operators:
Operators can even be replaced with most descriptive word.
# include<stdio.h>
# define AND &&
# define OR ||

void main ()
int a, b, c;
printf ("Enter the values for a, b, c\n");
scanf ("%d %d %d", &a, &b, &c);
if ((a<25) AND (b<5 OR c>10))
printf ("In the range \n");
printf ("Not in the range \n");


Define directive can be used even to replace a condition.
# include<stdio.h>
# define AND &&
# define RANGE (a>2 AND a<25)
void main ()
int a;
printf("Enter the values for a \n");
if (RANGE)
printf("In the range \n");
printf("Not in the range \n");


Define directive can be used to replace even an entire C statement.
# include <stdio.h>
# define WELCOME printf ("Welcome to M.G.M. \n");
void main ()

Macros with arguments:
Macros can have arguments just as functions can
# include<stdio.h>
# define AREA(x) (3.14 *x *x)
void main ()
float r, a;
printf ("Enter the value for radius \n");
scanf("%f", &r);
a =AREA(r);
printf("Area is % f\n", a);


In the above program, wherever the pre-processor finds AREA (x) it expands it into a statement (3.14*x*x).
The x in the macro template AREA (x) is an argument that matches x in the macro expansion.
The statement AREA (r) in the program causes the variable r to be substituted for x. Thus the statement AREA (r) is equivalent to (3.14 *r *r).

Although macro calls are function calls they are not really the same.
In a macro call, the pre-processor replaces the macro calls as it is.
Whereas in a function call the control is passed to a function along with certain arguments and a value may be returned by a function.
Usually macros make the program faster but increase the program size.
But functions make the program smaller and compact.
In functions, passing the arguments and getting back the result may take time and slow down the program. But the macros are expanded and kept.

File Inclusion:
These directives are used to include the files within a program. These are the directives, which we have been using in all our programs.
Using this directive one file is included in another file.

#include "file name"
#include <file name>

#include <stdio.h>
In the example the file stdio.h will be inserted in the file in which it is specified.

Compiler Control Directives:

These are the directives, which can be used for conditional compilation.
< br> The example given illustrates this:
# include <stdio.h>
# define i 1
# define j 5
# if i==1
# include <stdlib.h>
# else
# include<conio.h>
# endif
void main()
int k;
k=j * 10;
printf("calculated value of k is %d", k);

The example is just the illustration for usage of conditional compilation directives and does not carry much meaning.

C Fundamentals<< Previous Next >> C Storage Classes

Our aim is to provide information to the knowledge seekers.

Support us generously

comments powered by Disqus
Untitled Document
Untitled Document
Untitled Document