Vous êtes sur la page 1sur 27

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 :Header files and Conditional
Directives
Teaching Aids :PPTs, Animations
CM304.88 1
Recap

 What is a preprocessor?
 What are the different types of preprocessor
directives?

 What is a macro?
 What are the advantages of macros?

CM304.88 2
Objectives

On completion of this period, you would be able


to know …

 Header files.
 Defining Conditional compilation directives.

CM304.88 3
Header Files (#include)

Header files contain the following information


 Macros
 Constants
 Function prototypes
 Structure definitions

CM304.88 4
Header Files (#include)
Contd..
 The #include directive loads specified file
content in the current program.
 The macros and functions of loaded file can be
called in the current program.
 The included file is also compiled with current
program.
 Types of header files
1. predefined
2. user defined
CM304.88 5
Predefined header files
 Come with the compiler.
 Including a predefined header file in a program.
Syntax:
 1)#include<filename>
 2)#include”filename”
 < > means Preprocessor will search for the
file only in the standard directory.
 When the filename is included within “ “
means preprocessor will search for the file in
the current working directory and in the
standard directory.
CM304.88 6
Predefined header files

Example

 #include<stdio.h>

 #include<conio.h>

 #include<math.h>

 #include<string.h>

 #include<ctype.h>
CM304.88 7
User defined header file

 Created by the user using any editor.


 Including a user defined header file in a program.
 Syntax:
1)#include<filename>

2)#include”filename”

CM304.88 8
Example1

CM304.88 9
Example2

CM304.88 10
Conditional compilation directives

 1)#ifdef
Syntax: #ifdef IDENTIFIER
{
statement1;
statement2;
--------------;
--------------;
}
#endif

CM304.88 11
Conditional compilation directives
Contd..

 The #ifdef tests whether the IDENTIFIER is


defined or not.

 If the IDENTIFIER is defined then the


statements within { } are compiled and executed.

CM304.88 12
Example

CM304.88 13
#ifdef…#else…#endif

Syntax
#ifdef IDENTIFIER
{
Statement(s);
}
#else
{
Statement(s)
}
#endif

CM304.88 14
#ifdef…#else…#endif
Contd..

 If IDENTIFIER is defined then if block is

compiled and executed, else block is ignored.

 If IDENTIFIER is not defined then else block is

compiled and executed, if block is ignored.

CM304.88 15
Example1

CM304.88 16
Example2

CM304.88 17
#ifndef
Syntax:
#ifndef IDENTIFIER
{
statement(s)
}
#endif
If IDENTIFIER is not defined then statement(s)
is/are compiled and executed.

CM304.88 18
Example

CM304.88 19
Summary

In this class, you have learnt about …


 Header files contain static information.
 Usage of predefined header files.
 We can define our own header files.
 Conditional directives like #ifdef, #ifdef…#else--
#endif, #ifndef…#endif.

CM304.88 20
Quiz

1)sqrt() is predefined in _______Header file

a)conio.h

b)string.h

c)math.h

d)stdio.h

CM304.88 21
Quiz

1)sqrt() is predefined in _______Header file

a)conio.h

b)string.h

c)math.h

d)stdio.h

CM304.88 22
Quiz

2)String handiling functions are predefined in


______ header file

a)stdio.h

b)ctype.h

c)process.h

d)string.h
CM304.88 23
Quiz

2)String handiling functions are predefined in


______ header file

a)stdio.h

b)ctype.h

c)process.h

d)string.h
CM304.88 24
Quiz

3)Which of the following are conditional


compilation directives

a) #ifdef

b) #ifndef

c) #else

d) All the above


CM304.88 25
Quiz

3)Which of the following are conditional


compilation directives

a) #ifdef

b) #ifndef

c) #else

d) All the above


CM304.88 26
Frequently Asked Questions

1. Explain the #include directive with an example.

2. Explain the #if directive with an example.

3. Explain the #ifndef directive with an example.

CM304.88 27

Vous aimerez peut-être aussi