Exforsys.com
 
Home Tutorials C Language
 

C Language - The Preprocessor

 

C Language - The Preprocessor

In this tutorial you will learn about C Language - The Preprocessor, Preprocessor directives, Macros, #define identifier string, Simple macro substitution, Macros as arguments, Nesting of macros, Undefining a macro and File inclusion.

 

Sponsored Links

 

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.


Preprocessor directives follow the special syntax rules and begin with the symbol #bin column1 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


Undefines 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 division
2. File inclusion division
3. Compiler control division


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 int 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. Simple macro substitution
2. Argument macro substitution
3. Nested macro substitution


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


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)


Nesting of macros:

We can also use one macro in the definition of another macro. That is macro definitions may be nested. Consider the following macro definitions


# define SQUARE(x)((x)*(x))


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

#if constant expression

{
statement-1;
statemet2’
….
….
}
#endif

the constant expression can be a logical expression such as test < = 3 etc

 

Sponsored Links

 

br/> 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.



Read Next: Call by Value and Call by Reference



 

 

Comments


NATARAJ T.M said:

  IF U INCLUDE TWO SIMILAR HEDER FILE IN C
(i.e #include
#include
main()
{
}
)we wont get error, but it consumes lot of memory.
so that if u include two similar heder files, c preprocessor has to show error.
i need a code for getting above error in c.
October 25, 2006, 9:56 am

usha ranawade said:

  It is really good otes on preprocessor
I will like to see more examples.
November 28, 2006, 1:28 am

siba.s.nayak said:

  Nesting of macros:
We can also use one macro in the definition of another macro. That is macro definitions may be nested. Consider the following macro definitions

# define SQUARE(x)((x)*(x))

I think it is confusing what you mentioned in nesting of macros.I don't think it is nesting of macros.
I think the macro definition given below is more appropriate for nesting of Macros.
# define SQUARE(x)((x)*(x))
# define CUBE(x)(SQUARE(x)*(x))
January 11, 2007, 1:44 am

aks said:

  The description for #pragma and ## & # preprocessor operator is not mentioned in this tutorial. These should also be included.
June 23, 2007, 3:31 am

RajKumar.Sanpui said:

  I think the discussion would have been complete if you have added something on pragma directives and other important things as assert macros, as these kinds of tricky things are generally asked on interviews.
October 19, 2007, 2:51 am

krunal said:

  hey i wanna ppt on preprocesser archives
plz help
January 25, 2009, 4:04 am

ESWARAN said:

  i need detail explanaion about sizeof operator and data structures
January 30, 2009, 1:37 pm

Ram Mohan said:

  Why preprocessor directives are written as #include<stdio.h>,#include<conio.h>. why not as #include[stdio.h] or # include(stdio.h)? please give me the solution. I need it.
March 24, 2009, 10:56 am

balamurali.R said:

  Why preprocessor should come before source code?
preprocessor will come after the main line
the following program will execute:
main()
{
#include<stdio.h>
#include<conio.h>
printf("Intro to c peograms");
}
how preprocessor includes fiels(mechnism) in the source program??
i need more sample program...pls include
June 10, 2009, 1:29 pm

nafeesfathima said:

  what is the difference between #include<stdio.h> and #include"stdio.h"
June 17, 2009, 5:12 am

sourav bhattacharjee said:

  why # is required before #include<stdio.h>?
what is the function of #?
Please give me the solution.
July 19, 2009, 10:50 pm

ajay kumar mahore said:

  the # sign is a number symbol that contain the memory address of header file included through #include statement.
July 31, 2009, 7:39 am

amar kant tyagi said:

  I want that how we can scan keyword in C or total keywords in C program.
September 7, 2009, 11:36 pm

athachil said:

  Hi

Reply to nafeesfathima: what is the difference between #include and #include"stdio.h"?

I guess you are asking the difference between #include <stdio.h> and #include "stdio.h".
When you use <> symbol to include a file then it will search for that particular file in the standard directories of C compiler. ie. "include" folder in compiler directory
Where as if you use "", preprocessor will search for that file in user defined directories such as your current directory where your program is stored.

Answer to sourav bhattacharjee: why # is required before #include?

There is no big deal in it. Its just to inform the pre processor that whatever followed by # is a pre processor directive. Its just like we are using /* for a comment, then the compiler understands that everything before next */ are not part of C program syntax and skips the parsing.

Answer to ajay kumar mahore: the # sign is a number symbol that contain the memory address of header file included through #include statement.?

You are wrong. Preprocessor directives are not only used to include files. There are others such as #define, #ifndef, #ifdef, #endif, ##, #pragma etc..



October 23, 2009, 8:59 am

monica said:

  what is header file and which function of this file is used?
January 4, 2010, 11:25 am

Akhil said:

  I am a BSC Student and i want to know the below question answer/..

Q. Write a Program to find the area and perimiter of geometrical figure using macro defination.
January 7, 2010, 5:54 am

rume said:

  # include <stdio.h>
int main ( )
{
statement (s);
return 0;
}
February 4, 2010, 10:30 am

meghana said:

  what is the difference between getchar() and getche()?
February 10, 2010, 7:27 am

ramakant sahu said:

  why and when segmentation fault (core dumped) comes
February 16, 2010, 4:26 am

Mahesh lakher said:

  Q.what are difference between headerfile and liberary files
February 18, 2010, 2:29 am

ramagiripratap said:

  #define square(x) x*x
a = square(2+3)

answer??
it gives 11 how if u know plz mail me the procedure
March 15, 2010, 7:51 am

Mahesh V Deshpande said:

  Meghana answer for your question is defined well in the book "Let Us C".Not only your question but also most of the people will get answer in the book "Let us C".
March 30, 2010, 12:03 pm

JAISINGH said:

  In your website I studied c fundamentals which is best way to display in your page . I am really satisfied by your notes and in your notes I found only main categories which is require for us. Now I want list of programming with solutions and details also about that.
April 20, 2010, 5:41 am

ravi said:

  macro is just substitution so
square(2+3)---> 2+3*2+3=2+6+3=11
May 13, 2010, 2:18 pm

siva kotari said:

  what is difference between argument macro and nested macro....
# define CUBE (x) (x*x*x)
# define SQUARE(x)((x)*(x))
these are examples given by u at top.
both are similar hey clarify this man.......
July 20, 2010, 5:00 am

ramagiripratap got the answer said:

  #define square(x) (x*x)
a = square(2+3)
July 21, 2010, 2:33 pm

Kapil said:

  HEy,
How to create your own header file in C?
If anybody knows Please reply me.
July 21, 2010, 2:38 pm

vishu said:

  Please add seven steps of problem solving in C language
August 27, 2010, 9:44 am

abhishek garg said:

  How to find address of any function.
pls give me this tool.
August 31, 2010, 1:52 am

Shitesh Patel said:

  Give the working process and output of the following programme
#define DATATYPE char far*
main()
{
DATATYPE s;
s=0*b8000000;
*s='A';
}
September 2, 2010, 11:32 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape