You are on page 1of 23

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name :M.Subramanyam
Designation :Senior Lecturer
Branch :Computer Engg.
Institute :Q.Q.Govt.Polytechnic,Hyderabad.
Year/Semester :III Semester
Subject :UNIX & C
Subject Code :CM-304
Topic :Preprocessor directives
Duration :50 Min
Sub Topic :Types, Symbolic constants & Macros
Teaching Aids :PPTs, Animations

CM304.87 1
Recap

 How to include pre-defined header file.

 What is a compiler?

 What is a linker?

CM304.87 2
Objectives

On completion of this period, you would be able


to know…
 Introduction of preprocessor.
 Types of preprocessor directives.
 Defining a symbolic constants/macros.

CM304.87 3
Preprocessor

 Preprocessor is a program.

 Preprocessor processes the source program

before it is passed on to the compiler.

CM304.87 4
Compiling C Programs

C source code Preprocessor C Include files


files
.c files processed .h files
code

C compiler

Object code (.obj


Machine code .exe file
file)
.obj Executable
Libraries Linker
files CM304.87 code 5
Preprocessor directives

 Collection of special statements that are called


directives.
 Executed by the pre-processor.
 Occurs before a program is compiled.
 Begin with #.
 Don’t end with semicolon.
 Can be placed anywhere in the program.
 Normally placed at the beginning of the program
or before any particular function.
CM304.87 6
Advantages

Easy to

 Develop program
 Read programs
 Modify programs
 Portable
 Reuse
CM304.87 7
Types of preprocessor directives

They are 12 preprocessor directives


1) #include
2) #define
3) #if
4) #ifdef
5) #ifndef
6) #else
CM304.87 8
Types of preprocessor directives
Contd..

7) #elif
8) #endif
9) #undef
10) #error
11) #line
12) #pragma

CM304.87 9
Types of preprocessor directives

Preprocessor Meaning
Directive(s)
#include Treats text in the file specified by filename as if it
appeared in the current file.
#define Defines a macro/Symbolic constant.
#if , #ifdef, #ifndef, #else, Conditional compilation directives.

#elif, and #endif


#undef Undefines a symbol.

#error Issues error message.

#line Causes the compiler to think that the line number of


the next source line is given by <constant>, and the
current input file is given by <identifier>.
#pragma Implementation-specific directives.

CM304.87 10
The #define Preprocessor Directive

 1)Symbolic Constants

 To define symbolic constants.


Symbolic constant

Syntax: #define identifier replacementvalue

 When program is compiled, all occurrences


of symbolic constant will be replaced with

replacement value.
CM304.87 11
The #define Preprocessor Directive

Example: #define PI 3.14159

#define FALSE 0

To undefine a macro

E.g. #undef FALSE


 A macro must be undefined before being
redefined to a different value.
CM304.87 12
Example

CM304.87 13
The # define preprocessor directive

2)Macros
 #define IDENTIFIER(arg1,agr2,….agrn) body
 agr1,agr2…agrn are arguments, body is the
body of the macro.
 A macro without arguments is treated like a
symbolic constant.
 When the macro is called, the call is replaced
by its body.
 Well suited for small functions and functions
are called frequently.
CM304.87 14
The # define preprocessor directive
Contd..

Function Macro
Function call is nothing but Macro call is nothing but
branching. substitution.
Program execution is slow. Program execution is fast.

Processing is done by Processing is done by


CPU. preprocessor.
Program size is not Program size is increased.
increased.
CM304.87 15
The # define preprocessor directive
Contd..

Example body
Name argument

 #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
 Calling Macro
 area = CIRCLE_AREA( 4 );
to become
 area = ( 3.14159 * ( 4 ) * ( 4 ) );

CM304.87 16
Example

CM304.87 17
Nested Macros

 Macro within Macro is called as


Nested Macro.

CM304.87 18
Example

CM304.87 19
Summary

In this class, you have learnt about

 Types of preprocessor directives.

 Defining symbolic constants.

 Defining macros.

CM304.87 20
Quiz

1) Output of the following program


#define SQR(x) x*x
void main()
{
printf(“%d”,SQR(3+5));
}
a)23
b)64
c)8
d)none
CM304.87 21
Quiz

1) Output of the following program


#define SQR(x) x*x
void main()
{
printf(“%d”,SQR(3+5));
}
a)23
b)64
c)8
d)none
CM304.87 22
Frequently Asked Questions

1. List different preprocessor directives in C.

2. Explain #define directive with an example.

3. Distinguish between function and Macro.

CM304.87 23

You might also like