SlideShare una empresa de Scribd logo
1 de 36
Preprocessors
Introduction
Program that Processes or Analyzes the source code
file before given to the Compiler is called as Pre-
Processor.
Preprocessing Occurs before program compiled due
to
Inclusion of external files.
Definition of symbolic constants.
Macros.
Conditional compilation.
Conditional execution.
#include
Specifies that the preprocessor should read in the contents of the
specified file.
usually used to read in type definitions, prototypes, etc.
Two forms
#include <filename>
For standard library header files.
Searches pre designated directories.
#include "filename"
Searches in current directory.
Normally used for programmer-defined files.
User defined header files:
A header file has file-extension ‘.h’
These header files typically contain “public”
information like
type declarations
macros and other definitions
function prototypes
These header files are included by files that need
that public information
#include “myheaderfile.h”
#define
#define is the preprocessor directive for the macro.
Macros are generally used to define constant values
that are being used repeatedly in program.
Structure of simple macros:
#define identifier replacement_list
Examples:
#define BUFFERSZ 1024
#define WORDLEN 64
Using simple macros
We just use the macro name in place of the value, e.g.:
#define BUFLEN 1024
#define Pi 3.1416
…
char buffer[BUFLEN];
…
area = Pi * r * r;
Properties of macros
Takes no memory.
Name not be seen by debugger (only for
replacement of text).
Do not have specific data type.
Macro without arguments Treated like a symbolic
constant.
Macro with arguments Performs a text substitution.
No data type checking.
Macros may be nested.
Macro Vs. Function
Macro Function
Macro is Preprocessed. Function is Compiled.
No Type Checking . Type Checking is Done.
Speed of Execution is Faster. Speed of Execution is Slower.
Before Compilation macro name is
replaced by macro value.
During function call , Transfer of Control
takes place.
Useful where small code appears many
times.
Useful where large code appears many
times.
Generally Macros do not extend beyond
one line.
Function can be of any number of lines.
Macro does not Check Compile Errors. Function Checks Compile Errors.
Advanced Macro Tricks
The C pre processor offers following operators to help
you in creating macros:
Macro Continuation ()
Stringize (#)
Token Pasting (##)
Macro Continuation
The macro continuation operator is used to continue a
macro that is too long for a single line.'  ' indicate line
contuation.
The slash tells the preprocessor that the macro continuous
to the next line.
Ex:
#define SWAP(a, b) { 
a ^= b; 
b ^= a; 
a
^= b;  }
Stringize

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 that has
a specified argument or parameter list.
Example
#define STR(x) #x
#define XSTR(x) STR(x)
#define ONE 1
Macro call Result of Macro Expansion
STR(n "n" 'n') "n "n" 'n'“
STR(ONE) "ONE“
XSTR(ONE) "1“
XSTR("hello") ""hello""
Token Pasting
Token Pasting(##) permits separate tokens to be
joined into a single token. sometimes its called as
“Merging” operator.
#define ArgArg(x, y) x##y
#define Jitterbug 3
     Macro call Result of Macro Expansion
ArgArg(lady, bug) ladybug
ArgArg(Jitter, bug) 3
Predefined Macros
The standard predefined macros are specified by the
relevant language standards, so they are available
with all compilers that implement those standards.
We cannot use these predefined macros in #define
and #undef.
__FILE__: This macro expands to the name of the
current input file, in the form of a C string constant.
__LINE__: This macro expands to the current input
line number, in the form of a decimal integer constant.
__DATE__: This macro expands to a string constant
that describes the date on which the preprocessor is
being run. The string constant contains eleven
characters.
__TIME__: This macro expands to a string constant
that describes the time at which the preprocessor is
being run. The string constant contains eight
characters.
__STDC__: This macro expands to the constant 1, to
signify that this compiler conforms to ISO Standard
C.
__ASSEMBLER__: This macro is defined with value
1 when preprocessing assembly language.
#undef
Un defines symbolic constant or macro.
Can later be redefined.
#include <stdio.h>
#define value 100
void main()
{
printf("First defined value : %dn",value);
#undef value // undefining variable
#define value 600 // redefining the same for new value
printf("value after undef & redefine:%d",value);
}
Conditional Compilation
Conditional Compilation Directives allow us to
include certain portion of the code depending upon
the output of constant expression.
Conditional Compilation Directives are #if, #else,
#endif, #ifdef, #ifndef,#pragma,#undef.
Every #if ends with #endif.
#elif is "else if”.
#ifdef short for #if defined(name).
#ifndef short for #if !defined(name).
#if
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file);
#else
printf("This line will not be added in this c file");
#endif
return 0;
}
Commenting the code
#if 0
code commented out
#endif
To enable code, change the value from 0
to 1
#ifdef
#include <stdio.h>
#define VALUE 100
int main()
{
#ifdef VALUE
printf("VALUE is definedn");
#else
printf("VALUE is not definedn");
#endif
return 0;
}
#ifndef
#include <stdio.h>
#define A 100
int main()
{
#ifndef B{
printf("B is not defined");
#define B 300
}
#else
printf("B is already defined”);
#endif
return 0;
}
Line numbers
#line is used to renumbers subsequent code
lines, starting with integer.
#line 100 means next source code line is
numbered as 100.
For error purposes we will use this line
numbers.
Can make syntax errors more meaningful.
Line numbers do not appear in source file.
Example
#include <stdio.h>
#define LINE200 200
int main(void){
func_1();
func_2();
}
#line 100
func_1(){
printf("Func_1 - the current line number is %dn",_ _LINE_ _);
}
#line LINE200
func_2(){
printf("Func_2 - the current line number is %dn",_ _LINE_ _);
}
#error
The #error directive causes the preprocess to print a
diagnostic error message, using the argument as a part
of the message.
Useful to trap incorrect conditions in conditional
compilation.
Compilation is aborted when this directive is
invoked.
Example
#ifndef __MATH_H
#error First include then compile
#else
void main()
{
float a,b=25;
a=sqrt(b);
printf(“%f”,a);
}
#endif
Result: First include then compile
#pragma
#pragma directive is used to control the actions of
the compiler in a particular portion of a program
without affecting the program.
pragma directive varies from one compiler to
another compiler .
If compiler does not recognize particular pragma
then it ignore the pragma statement without showing
any error or warning message.
We can aviod the structure padding using #pragma.
The default compiler alignment is of 4 bytes. we
can use #pragma to change the byte alignment to 1
byte.
For that, do the following steps:
1.Push the current compiler alignment into the stack.
2.Set the alignment into 1 byte.
3.Declare the structure.
4.Restore the default compiler alignment from the
stack.
Example
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
struct EX
{
char a;
short int b;
char c;
int d;
};
#pragma pack(pop) /* restore original alignment from stack */.
Without #pragma,the size of the structure is 12 bytes.
With #pragma,the size of the structure is 8 byets.
gcc -fpack-struct filename.c :
This is command for remove the struct padding at compilation time .
#pragma startup & exit
pragma startup always execute the function before the main
function.
pragma exit always execute the function after the main
function.
If more than one startup directive then priority decides
which will execute first.
For startup, Lower value will have higher priority i.e
function will execute first.
If more than one exit directive then priority decides which
will execute first.
For exit, Higher value will have higher priority i.e function
will execute first.
Example
void fun1();
void fun2() ;
#pragma startup fun1 105
#pragma startup fun2
#pragma exit fun2
#pragma exit fun1 105
void main(){
printf("nI am in main");
}
void fun1(){
printf("nI am in fun1");
}
void fun2(){
printf("nI am in fun2");
}
#pragma warn
In c there are many warning messages which can be on or
off with help of #pragma warn.
#pragma warn +xxx
#pragma warn –xxx
#pragma warn .xxx
Where + means on
-means off
. means on/off (toggle)
xxx will indicate particular warning code in thee alphabet.
Example
#pragma warn –rvl
int main()
{
Printf(“It will not show any warning message”);
}
Output: It will not show any warning message.
When you will execute the above program then
compiler will not show the warning message like
function should return a value.
Assertions
assert is a macro, Header is <cassert> .
Tests value of an expression.
If 0 (false) prints error message, calls abort terminates
program, prints line number and file.
Good for checking for illegal values.
If 1 (true), program continues as normal
assert( x <= 10 );
To remove assert statements No need to delete them
manually
#define NDEBUG
All subsequent assert statements ignored.
Preprocessors: Introduction to #include, #define, Conditional Compilation

Más contenido relacionado

La actualidad más candente

Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function OverloadingMeghaj Mallick
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c languagekamalbeydoun
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareGagan Deep
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 

La actualidad más candente (20)

C functions
C functionsC functions
C functions
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Function in c
Function in cFunction in c
Function in c
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 

Similar a Preprocessors: Introduction to #include, #define, Conditional Compilation

Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproceHector Garzo
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfacsmadurai
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptxAlAmos4
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 

Similar a Preprocessors: Introduction to #include, #define, Conditional Compilation (20)

Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
C programming session6
C programming  session6C programming  session6
C programming session6
 
Chapter3
Chapter3Chapter3
Chapter3
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 

Más de Koganti Ravikumar (7)

Qemu
QemuQemu
Qemu
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Loaders
LoadersLoaders
Loaders
 
Linkers
LinkersLinkers
Linkers
 
Linker scripts
Linker scriptsLinker scripts
Linker scripts
 
ELF
ELFELF
ELF
 

Último

VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja Nehwal
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxjeswinjees
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Servicearoranaina404
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...amitlee9823
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdftbatkhuu1
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneLukeKholes
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...babafaisel
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...RitikaRoy32
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 

Último (20)

VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 

Preprocessors: Introduction to #include, #define, Conditional Compilation

  • 2. Introduction Program that Processes or Analyzes the source code file before given to the Compiler is called as Pre- Processor. Preprocessing Occurs before program compiled due to Inclusion of external files. Definition of symbolic constants. Macros. Conditional compilation. Conditional execution.
  • 3.
  • 4.
  • 5. #include Specifies that the preprocessor should read in the contents of the specified file. usually used to read in type definitions, prototypes, etc. Two forms #include <filename> For standard library header files. Searches pre designated directories. #include "filename" Searches in current directory. Normally used for programmer-defined files.
  • 6. User defined header files: A header file has file-extension ‘.h’ These header files typically contain “public” information like type declarations macros and other definitions function prototypes These header files are included by files that need that public information #include “myheaderfile.h”
  • 7. #define #define is the preprocessor directive for the macro. Macros are generally used to define constant values that are being used repeatedly in program. Structure of simple macros: #define identifier replacement_list Examples: #define BUFFERSZ 1024 #define WORDLEN 64
  • 8. Using simple macros We just use the macro name in place of the value, e.g.: #define BUFLEN 1024 #define Pi 3.1416 … char buffer[BUFLEN]; … area = Pi * r * r;
  • 9. Properties of macros Takes no memory. Name not be seen by debugger (only for replacement of text). Do not have specific data type. Macro without arguments Treated like a symbolic constant. Macro with arguments Performs a text substitution. No data type checking. Macros may be nested.
  • 10. Macro Vs. Function Macro Function Macro is Preprocessed. Function is Compiled. No Type Checking . Type Checking is Done. Speed of Execution is Faster. Speed of Execution is Slower. Before Compilation macro name is replaced by macro value. During function call , Transfer of Control takes place. Useful where small code appears many times. Useful where large code appears many times. Generally Macros do not extend beyond one line. Function can be of any number of lines. Macro does not Check Compile Errors. Function Checks Compile Errors.
  • 11. Advanced Macro Tricks The C pre processor offers following operators to help you in creating macros: Macro Continuation () Stringize (#) Token Pasting (##)
  • 12. Macro Continuation The macro continuation operator is used to continue a macro that is too long for a single line.' ' indicate line contuation. The slash tells the preprocessor that the macro continuous to the next line. Ex: #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
  • 13. Stringize  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 that has a specified argument or parameter list.
  • 14. Example #define STR(x) #x #define XSTR(x) STR(x) #define ONE 1 Macro call Result of Macro Expansion STR(n "n" 'n') "n "n" 'n'“ STR(ONE) "ONE“ XSTR(ONE) "1“ XSTR("hello") ""hello""
  • 15. Token Pasting Token Pasting(##) permits separate tokens to be joined into a single token. sometimes its called as “Merging” operator. #define ArgArg(x, y) x##y #define Jitterbug 3      Macro call Result of Macro Expansion ArgArg(lady, bug) ladybug ArgArg(Jitter, bug) 3
  • 16. Predefined Macros The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. We cannot use these predefined macros in #define and #undef. __FILE__: This macro expands to the name of the current input file, in the form of a C string constant. __LINE__: This macro expands to the current input line number, in the form of a decimal integer constant.
  • 17. __DATE__: This macro expands to a string constant that describes the date on which the preprocessor is being run. The string constant contains eleven characters. __TIME__: This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters. __STDC__: This macro expands to the constant 1, to signify that this compiler conforms to ISO Standard C. __ASSEMBLER__: This macro is defined with value 1 when preprocessing assembly language.
  • 18. #undef Un defines symbolic constant or macro. Can later be redefined. #include <stdio.h> #define value 100 void main() { printf("First defined value : %dn",value); #undef value // undefining variable #define value 600 // redefining the same for new value printf("value after undef & redefine:%d",value); }
  • 19. Conditional Compilation Conditional Compilation Directives allow us to include certain portion of the code depending upon the output of constant expression. Conditional Compilation Directives are #if, #else, #endif, #ifdef, #ifndef,#pragma,#undef. Every #if ends with #endif. #elif is "else if”. #ifdef short for #if defined(name). #ifndef short for #if !defined(name).
  • 20. #if #include <stdio.h> #define a 100 int main() { #if (a==100) printf("This line will be added in this C file); #else printf("This line will not be added in this c file"); #endif return 0; }
  • 21. Commenting the code #if 0 code commented out #endif To enable code, change the value from 0 to 1
  • 22. #ifdef #include <stdio.h> #define VALUE 100 int main() { #ifdef VALUE printf("VALUE is definedn"); #else printf("VALUE is not definedn"); #endif return 0; }
  • 23. #ifndef #include <stdio.h> #define A 100 int main() { #ifndef B{ printf("B is not defined"); #define B 300 } #else printf("B is already defined”); #endif return 0; }
  • 24. Line numbers #line is used to renumbers subsequent code lines, starting with integer. #line 100 means next source code line is numbered as 100. For error purposes we will use this line numbers. Can make syntax errors more meaningful. Line numbers do not appear in source file.
  • 25. Example #include <stdio.h> #define LINE200 200 int main(void){ func_1(); func_2(); } #line 100 func_1(){ printf("Func_1 - the current line number is %dn",_ _LINE_ _); } #line LINE200 func_2(){ printf("Func_2 - the current line number is %dn",_ _LINE_ _); }
  • 26. #error The #error directive causes the preprocess to print a diagnostic error message, using the argument as a part of the message. Useful to trap incorrect conditions in conditional compilation. Compilation is aborted when this directive is invoked.
  • 27. Example #ifndef __MATH_H #error First include then compile #else void main() { float a,b=25; a=sqrt(b); printf(“%f”,a); } #endif Result: First include then compile
  • 28. #pragma #pragma directive is used to control the actions of the compiler in a particular portion of a program without affecting the program. pragma directive varies from one compiler to another compiler . If compiler does not recognize particular pragma then it ignore the pragma statement without showing any error or warning message.
  • 29. We can aviod the structure padding using #pragma. The default compiler alignment is of 4 bytes. we can use #pragma to change the byte alignment to 1 byte. For that, do the following steps: 1.Push the current compiler alignment into the stack. 2.Set the alignment into 1 byte. 3.Declare the structure. 4.Restore the default compiler alignment from the stack.
  • 30. Example #pragma pack(push) /* push current alignment to stack */ #pragma pack(1) /* set alignment to 1 byte boundary */ struct EX { char a; short int b; char c; int d; }; #pragma pack(pop) /* restore original alignment from stack */. Without #pragma,the size of the structure is 12 bytes. With #pragma,the size of the structure is 8 byets. gcc -fpack-struct filename.c : This is command for remove the struct padding at compilation time .
  • 31. #pragma startup & exit pragma startup always execute the function before the main function. pragma exit always execute the function after the main function. If more than one startup directive then priority decides which will execute first. For startup, Lower value will have higher priority i.e function will execute first. If more than one exit directive then priority decides which will execute first. For exit, Higher value will have higher priority i.e function will execute first.
  • 32. Example void fun1(); void fun2() ; #pragma startup fun1 105 #pragma startup fun2 #pragma exit fun2 #pragma exit fun1 105 void main(){ printf("nI am in main"); } void fun1(){ printf("nI am in fun1"); } void fun2(){ printf("nI am in fun2"); }
  • 33. #pragma warn In c there are many warning messages which can be on or off with help of #pragma warn. #pragma warn +xxx #pragma warn –xxx #pragma warn .xxx Where + means on -means off . means on/off (toggle) xxx will indicate particular warning code in thee alphabet.
  • 34. Example #pragma warn –rvl int main() { Printf(“It will not show any warning message”); } Output: It will not show any warning message. When you will execute the above program then compiler will not show the warning message like function should return a value.
  • 35. Assertions assert is a macro, Header is <cassert> . Tests value of an expression. If 0 (false) prints error message, calls abort terminates program, prints line number and file. Good for checking for illegal values. If 1 (true), program continues as normal assert( x <= 10 ); To remove assert statements No need to delete them manually #define NDEBUG All subsequent assert statements ignored.