SlideShare uma empresa Scribd logo
1 de 30
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,#pragma-startup,exit,warn)
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.
Difference between Macro &
Function
 I)In a macro call,the preprocessor
replaces the macro template with its
macro exapansion only.whereas, in a
functional call the control is passed to
a function along with certain
arguments,some calculations are
performed in the function& a useful
value is returned back from the
function.
 II) if we use a macro hundred times in
a program,the macro expansion goes
into our source code at hundred
different palaces,thus increasing the
program size. On the other hand,if a
function is used,then even if it is called
from hundred different places in the
program,it would take the same
amount of space in the program,thus
making the prigram smaller and
compact.
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”
 #include “goto.h”: It searches for a file
named “goto.h” in the directory
containing the current file.
 #include<goto.h>: it searches for a file
named “goto.h” in a standard list of
system directories.
Undefining a Macro
 The #undef preprocessor directive is
used to undefine the constant or
macro defined by #define.
 Syntax:
 Example:
Preprocessor Operators
 The C preprocessor offers the
following operators to help create
macros −
I)The Macro Continuation () Operator
 A macro is normally confined to a
single line. The macro continuation
operator () is used to continue a
macro that is too long for a single line.
 For example −
II)The Stringize (#) Operator
 The stringize or number-sign operator
('#'), when used within a macro definition,
converts a macro parameter into a string
constant.
 This operator may be used only in a
macro having a specified argument or
parameter list.
 For example −
# define say(m) printf(#m)
void main()
{
clrscr(); Output:
say(Hello); Hello
getche();
}
III)The Token Pasting (##) Operator
 The token-pasting operator (##) within
a macro definition combines two
arguments.
 It permits two separate tokens in the
macro definition to be joined into a
single token.
 For example −
When the above code is compiled and executed, it
produces the following result −
It happened so because this example results in the
following actual output from the preprocessor −
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:
#pragma directive
 The #pragma preprocessor directive is
used to provide additional information
to the compiler.
 The #pragma directive is used by the
compiler to offer machine or
operating-system feature.
 Syntax:
Example:
 Syntax:
#pragma startup <function_name>
 startup pragma allow the program to specify function(s) that
should be called upon program startup
 Function is called before main().
 Syntax :
#pragma exit <function_name>
 exit pragma allow the program to specify function(s) that
should be called just before the program terminates through
_exit.
 Function is called after main().
 <function_name> must be a previously declared function
that takes no arguments and returns void.
 It should be declared as –
 void func(void);
 The function name must be defined (or declared) before the
pragma line is reached.
# error directive
 The #error preprocessor directive
indicates error.
 The compiler gives fatal error if #error
directive is found and skips further
compilation process.
Example:

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Preprocessors
Preprocessors Preprocessors
Preprocessors
 
Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Function in C
Function in CFunction in C
Function in C
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Strings in C
Strings in CStrings in C
Strings in C
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Call by value
Call by valueCall by value
Call by value
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Structure in C
Structure in CStructure in C
Structure in C
 

Semelhante a Preprocessor directives in c language

Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptxAlAmos4
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macrosAnand Kumar
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsssuserf86fba
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11patcha535
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproceHector Garzo
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Preprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishPreprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishMuhammed Thanveer M
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 

Semelhante a Preprocessor directives in c language (20)

C programming session6
C programming  session6C programming  session6
C programming session6
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11
 
Preprocessor.pptx
Preprocessor.pptxPreprocessor.pptx
Preprocessor.pptx
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Preprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishPreprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danish
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
interview questions.docx
interview questions.docxinterview questions.docx
interview questions.docx
 

Mais de tanmaymodi4

Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1tanmaymodi4
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c languagetanmaymodi4
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 

Mais de tanmaymodi4 (9)

Cryptocurrency
CryptocurrencyCryptocurrency
Cryptocurrency
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 

Último

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Último (20)

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

Preprocessor directives in c language

  • 2. 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.
  • 3. Preprocessor directives are executed before compilation.
  • 4.  The various preprocessor directives are as follows: 1)Macro Expansion (#define) II)File Inclusion(#include) III)Conditional Compilation(#ifdef -#endif) IV)Miscellaneous Directives (#undef,#pragma-startup,exit,warn)
  • 5. 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.
  • 6. 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
  • 7. 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); }
  • 8. 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 ||
  • 9. 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”)
  • 10. 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
  • 11. Macro with arguments  Function –like macros can take arguments just like true functions.
  • 12. Difference between Macro & Function  I)In a macro call,the preprocessor replaces the macro template with its macro exapansion only.whereas, in a functional call the control is passed to a function along with certain arguments,some calculations are performed in the function& a useful value is returned back from the function.
  • 13.  II) if we use a macro hundred times in a program,the macro expansion goes into our source code at hundred different palaces,thus increasing the program size. On the other hand,if a function is used,then even if it is called from hundred different places in the program,it would take the same amount of space in the program,thus making the prigram smaller and compact.
  • 14. 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.
  • 15. 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”
  • 16.  #include “goto.h”: It searches for a file named “goto.h” in the directory containing the current file.  #include<goto.h>: it searches for a file named “goto.h” in a standard list of system directories.
  • 17. Undefining a Macro  The #undef preprocessor directive is used to undefine the constant or macro defined by #define.  Syntax:  Example:
  • 18. Preprocessor Operators  The C preprocessor offers the following operators to help create macros − I)The Macro Continuation () Operator  A macro is normally confined to a single line. The macro continuation operator () is used to continue a macro that is too long for a single line.  For example −
  • 19. II)The Stringize (#) Operator  The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant.  This operator may be used only in a macro having a specified argument or parameter list.  For example − # define say(m) printf(#m) void main() { clrscr(); Output: say(Hello); Hello getche(); }
  • 20. III)The Token Pasting (##) Operator  The token-pasting operator (##) within a macro definition combines two arguments.  It permits two separate tokens in the macro definition to be joined into a single token.  For example −
  • 21. When the above code is compiled and executed, it produces the following result − It happened so because this example results in the following actual output from the preprocessor −
  • 22. 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:
  • 24. #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:
  • 26. #pragma directive  The #pragma preprocessor directive is used to provide additional information to the compiler.  The #pragma directive is used by the compiler to offer machine or operating-system feature.  Syntax:
  • 28.  Syntax: #pragma startup <function_name>  startup pragma allow the program to specify function(s) that should be called upon program startup  Function is called before main().  Syntax : #pragma exit <function_name>  exit pragma allow the program to specify function(s) that should be called just before the program terminates through _exit.  Function is called after main().  <function_name> must be a previously declared function that takes no arguments and returns void.  It should be declared as –  void func(void);  The function name must be defined (or declared) before the pragma line is reached.
  • 29. # error directive  The #error preprocessor directive indicates error.  The compiler gives fatal error if #error directive is found and skips further compilation process.