0% found this document useful (0 votes)
3 views18 pages

Pre Processor Directives

The document explains preprocessor directives in C, which are commands processed before compilation, starting with the # symbol. Key directives include macro expansion, file inclusion, and conditional compilation, each serving different purposes such as defining constants or including files. The document provides examples and syntax for using these directives effectively in C programming.

Uploaded by

shanmukhgara48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Pre Processor Directives

The document explains preprocessor directives in C, which are commands processed before compilation, starting with the # symbol. Key directives include macro expansion, file inclusion, and conditional compilation, each serving different purposes such as defining constants or including files. The document provides examples and syntax for using these directives effectively in C programming.

Uploaded by

shanmukhgara48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Preprocessor Directives in

C
Definition
A preprocessor is a program that
processes our source program before it
is passed to the compiler.
The preprocessor works on the source
code and creates “expanded source
code”.
The preprocessor offers several features
called preprocessor directives.
Each of theses preprocessor directives
begin with a # symbol.
The directives can be placed anywhere
in a program but most often placed at the
beginning of the program,before the
function definition.
Preprocessor directives are executed before
compilation.
The various preprocessor directives
are as follows:
1)Macro Expansion (#define)
II)File Inclusion(#include)
III) Conditional Compilation(#ifdef -#endif)
IV)Miscellaneous Directives
(#undef)
Macro Expansion
A “macro” is a fragment of code which
has been given a name.
Wherever the name is used,it is
replaced by the contents of the macro.
There are two kinds of macros which
are as follows:
I) Object-like macros resemble data
objects
II)Function-like macros resemble function
calls.
Object –like Macros
The object-like macro is an identifier
that is replaced by value.
It is widely used to represent numeric
constants.
It is called object-like because it looks
like a data object in code that uses it.
Syntax:
#define identifier replacement-text
Examples
#define x 4 x=identifier 4=replacement-text
void main()
{
int a;
a=x;
}

#define pi 3.14 pi=identifier


3.14=substitute
void main()
{
float r=6.25,area;
area=pi*r*r;
printf(“Area of circle=%f”,area);
}
Note:
To create macros with the #define
directive,the syntax for macros is:
#define macro macro
expansion replacement list

#define directive can be used as follows:


I) It can be used to define operators
like: #define AND &&
#define OR ||
II) it could be used even to replace a
condition:
# define AND &&
#define ARRANGE (a>25 AND a<50)
III) It could be used to replace even an
entire C statement like:
# define FOUND printf(“Hello World”)
Function-like Macro
It looks like a function call & can be
defined with the # define directive but
with a pair of braces or parenthesis
immediately after the macro name.
For Example:
#define Area(x) 3.14* r*r
Macro with arguments
Function –like macros can take
arguments just like true functions.
File Inclusion
Used to include one file into another file
or it simply causes the entire content of
one file to be inserted into the source
code of another file.
We use file inclusions for the following
reasons:
I)if we have a very large program, the
code is best divided into several different
files,each containing a set of related
functions.These files are then included at
the beginning of main program file.
II)There are some commonly needed
functions & macro definitions that can
be stored in a file &that file can be
included in every program we write
which would add all the statements in
this file to our program as if we have
typed in.
The two ways to write include
statement:
#include “filename”
Undefining a Macro
The #undef preprocessor directive is
used to undefine the constant or
macro defined by #define.
Syntax:

Example:
Conditional Compilation
These allow us to include certain
portion of the code depending upon
the output of constant expression.
The #ifdef preprocessor directive
checks if macro is defined by #define.
If yes, it executes the code otherwise
#else code is executed, if present.
Syntax:
Example:
#ifndef directive
The #ifndef preprocessor directive
checks if macro is not defined by
#define.
If yes, it executes the code otherwise
#else code is executed, if present.
Syntax:
Example:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy