SlideShare une entreprise Scribd logo
1  sur  28
Anu. S
anusdhar4@gmail.com
www.facebook.com/AnuSasidharan
twitter.com/username
in.linkedin.com/in/profilename
9400892764
Pre-Processor Directives in C
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
INTRODUCTION
The C preprocessor executes before a program
is compiled.
Some actions it performs are
• inclusion of other files in the file being
compiled
•definition of symbolic constants and macros
•conditional compilation of program code
Preprocessor directives begin with #.
#include
It causes a copy of a specified file to be included in place of the directive.
The two forms of the #include directive are:
#include <filename>
#include "filename”
The difference between these is in the location the preprocessor begins
searches for the file to be included.
If the file name is enclosed in quotes, the preprocessor starts searches in
the same directory as the file being compiled for the file to be included .
This method is normally used to include programmer-defined headers.
If the file name is enclosed in angle brackets is used for standard library
header.
The search is performed in an implementation-dependent manner,
normally through predesignated compiler and system directories.
EXAMPLE
#include<stdio.h>
#include<string.h>
main()
{
int n;
char s[10];
printf("nEnter the string:n");
scanf("%s",s);
n=strlen(s);
printf("nLength is:%d",n);
}
EXAMPLE
//arithmetic.h file
int sum(int int_a,int int_b)
{ int add=0;
add=int_a+int_b;
return(add); }
int sub(int int_a,int int_b)
{ int diff=0;
diff= int_a-int_b;
return(diff); }
int mul(int int_a,int int_b)
{ int pro;
pro= int_a*int_b;
return(pro) }
int div(int int_a,int int_b)
{
int quo;
quo= int_a/int_b; }
EXAMPLE
#include<stdio.h>
#include "arithmetic.h"
main()
{
int s,a,b;
scanf("%d%d",&a,&b);
s=sum(a,b);
printf("nSum=%d",n);
}
#define
The #define directive creates
•symbolic constants
•constants represented as symbols
•macros operations defined as symbols.
The #define directive format is
#define identifier replacement-text
When this line appears in a file, all subsequent
occurrences of identifier will be replaced by replacement-
text automatically before the program is compiled.
#define: symbolic constants
For example,
#define PI 3.14159
replaces all subsequent occurrences of the symbolic
constant PI with the numeric constant 3.14159.
Symbolic constants enable you to create a name for a
constant and use the name throughout the program.
If the constant needs to be modified throughout the program,
it can be modified once in the #define directive.
When the program is recompiled, all occurrences of the
constant in the program will be modified accordingly.
#define: macros
A macro is an identifier defined in a #define preprocessor
directive.
As with symbolic constants, the macro-identifier is
replaced in the program with the replacement-text before the
program is compiled.
Macros may be defined with or without arguments.
A macro without arguments is processed like a symbolic
constant.
In a macro with arguments, the arguments are substituted
in the replacement text, then the macro is expanded.
#define: macros
A symbolic constant is a type of macro.
Consider the following macro definition with one argument for
the area of a circle:
#define CIRCLE_AREA( x ) ( ( PI ) * ( x ) * ( x ) )
Wherever CIRCLE_AREA(y) appears in the file, the value of y
is substituted for x in the replacement-text, the symbolic constant
PI is replaced by its value (defined previously) and the macro is
expanded in the program.
The parentheses around each x in the replacement text force the
proper order of evaluation when the macro argument is an
expression.
EXAMPLE
•For example, the statement
area = CIRCLE_AREA( 4 );
is expanded to
area = ( ( 3.14159 ) * ( 4 ) * ( 4 ) );
•For example, the statement
area = CIRCLE_AREA( c + 2 );
is expanded to
area = (( 3.14159 ) * ( c + 2 ) * ( c + 2 ));
which evaluates correctly because the
parentheses force the proper order of evaluation.
#define: macros
If the parentheses are omitted, the macro expansion
is
area = 3.14159 * c + 2 * c + 2;
which evaluates incorrectly as
area = ( 3.14159 * c ) + ( 2 * c ) + 2;
because of the rules of operator precedence.
#define: macros
•Symbolic constants and macros can be discarded by using
the #undef preprocessor directive.
•Directive #undef “undefines” a symbolic constant or macro
name.
•The scope of a symbolic constant or macro is from its
definition until it is undefined with #undef, or until the end
of the file.
•A macro must be undefined before being redefined to a
different value.
•Once undefined, a name can be redefined with #define.
CONDITIONAL COMPILATION
•Conditional compilation enables you to control the
execution of preprocessor directives and the compilation of
program code.
•Each of the conditional preprocessor directives evaluates a
constant integer expression.
•The conditional preprocessor construct is much like the if
selection statement.
CONDITIONAL COMPILATION
•Consider the following preprocessor code:
#if !defined(MY_CONSTANT)
#define MY_CONSTANT 0
#endif
•These directives determine if MY_CONSTANT is defined.
•The expression defined( MY_CONSTANT) evaluates to 1
if MY_CONSTANT is defined; 0 otherwise.
•If the result is 0, !defined(MY_CONSTANT) evaluates to
1 and MY_CONSTANT is defined.
•Otherwise, the #define directive is skipped.
CONDITIONAL COMPILATION
•Every #if construct ends with #endif.
•Directives #ifdef and #ifndef are shorthand for #if defined
and #if !defined.
•During program development, it is often helpful to
“comment out” portions of code to prevent it from being
compiled.
•We can use the following preprocessor construct for the
same:
#if 0
code prevented from compiling
#endif
•To enable the code to be compiled, replace the 0 in the
preceding construct with 1.
#error Pre Processor Directives
•The #error directive
#error tokens
prints an implementation-dependent message
including the tokens specified in the directive.
•The tokens are sequences of characters separated by spaces.
•For example,
#error 1 - Out of range error
•When a #error directive is processed on some systems, the
tokens in the directive are displayed as an error message,
preprocessing stops and the program does not compile.
EXAMPLE
#ifdef OS_MSDOS
#include <msdos.h>
#elifdef OS_UNIX
#include "default.h"
#else
#error Wrong OS!!
#endif
•The # and ## preprocessor operators are available in
Standard C.
•The # operator causes a replacement text token to be
converted to a string surrounded by quotes.
•Example:
#define HELLO(x) printf( "Hello, " #x "n" );
•When HELLO(John) appears in a program file, it is
expanded to
printf( "Hello, " "John" "n" );
•The string "John" replaces #x in the replacement text.
# and # # OPERATORS
•Strings separated by white space are concatenated during
preprocessing, so the preceding statement is equivalent to
printf( "Hello, Johnn" );
•The # operator must be used in a macro with arguments
because the operand of # refers to an argument of the macro.
# and # # OPERATORS
•Consider the following macro definition:
#define TOKENCONCAT(x, y) x ## y
•When TOKENCONCAT appears in the program, its
arguments are concatenated and used to replace the macro.
•For example, TOKENCONCAT(O, K) is replaced by OK in
the program.
•The ## operator must have two operands.
# and # # OPERATORS
•The #line preprocessor directive causes the
subsequent source code lines to be renumbered
starting with the specified constant integer value.
•The directive
#line 100
starts line numbering from 100 beginning with
the next source code line.
•A file name can be included in the #line directive.
LINE NUMBERS
THANK YOU...
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

Contenu connexe

Tendances

constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
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 CTushar B Kute
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
Conditional operators
Conditional operatorsConditional operators
Conditional operatorsBU
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILEDipta Saha
 
C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variablessangrampatil81
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 

Tendances (20)

constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
 
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
 
Type conversion
Type  conversionType  conversion
Type conversion
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
C++
C++C++
C++
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

En vedette

En vedette (18)

The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
 
Macro
MacroMacro
Macro
 
Cp0675 03 may-2012-rm04
Cp0675 03 may-2012-rm04Cp0675 03 may-2012-rm04
Cp0675 03 may-2012-rm04
 
Lecture 4 assembly language
Lecture 4   assembly languageLecture 4   assembly language
Lecture 4 assembly language
 
What is c
What is cWhat is c
What is c
 
SAS Macros
SAS MacrosSAS Macros
SAS Macros
 
System programming
System programmingSystem programming
System programming
 
Ss4
Ss4Ss4
Ss4
 
Assembly Language Lecture 2
Assembly Language Lecture 2Assembly Language Lecture 2
Assembly Language Lecture 2
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Assembly Language Lecture 3
Assembly Language Lecture 3Assembly Language Lecture 3
Assembly Language Lecture 3
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 

Similaire à Pre processor directives in c

Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptxAlAmos4
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11patcha535
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macrosAnand Kumar
 
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
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
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 to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)mujeeb memon
 

Similaire à Pre processor directives in c (20)

C programming session6
C programming  session6C programming  session6
C programming session6
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11
 
Preprocesser in c
Preprocesser in cPreprocesser in c
Preprocesser in c
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
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
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Preprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishPreprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danish
 
Preprocessor.pptx
Preprocessor.pptxPreprocessor.pptx
Preprocessor.pptx
 
Rr
RrRr
Rr
 
Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)
 
C pdf
C pdfC pdf
C pdf
 

Plus de baabtra.com - No. 1 supplier of quality freshers

Plus de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Dernier

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Dernier (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

Pre processor directives in c

  • 1.
  • 3. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 4. INTRODUCTION The C preprocessor executes before a program is compiled. Some actions it performs are • inclusion of other files in the file being compiled •definition of symbolic constants and macros •conditional compilation of program code Preprocessor directives begin with #.
  • 5. #include It causes a copy of a specified file to be included in place of the directive. The two forms of the #include directive are: #include <filename> #include "filename” The difference between these is in the location the preprocessor begins searches for the file to be included. If the file name is enclosed in quotes, the preprocessor starts searches in the same directory as the file being compiled for the file to be included . This method is normally used to include programmer-defined headers. If the file name is enclosed in angle brackets is used for standard library header. The search is performed in an implementation-dependent manner, normally through predesignated compiler and system directories.
  • 6. EXAMPLE #include<stdio.h> #include<string.h> main() { int n; char s[10]; printf("nEnter the string:n"); scanf("%s",s); n=strlen(s); printf("nLength is:%d",n); }
  • 7. EXAMPLE //arithmetic.h file int sum(int int_a,int int_b) { int add=0; add=int_a+int_b; return(add); } int sub(int int_a,int int_b) { int diff=0; diff= int_a-int_b; return(diff); } int mul(int int_a,int int_b) { int pro; pro= int_a*int_b; return(pro) } int div(int int_a,int int_b) { int quo; quo= int_a/int_b; }
  • 9. #define The #define directive creates •symbolic constants •constants represented as symbols •macros operations defined as symbols. The #define directive format is #define identifier replacement-text When this line appears in a file, all subsequent occurrences of identifier will be replaced by replacement- text automatically before the program is compiled.
  • 10. #define: symbolic constants For example, #define PI 3.14159 replaces all subsequent occurrences of the symbolic constant PI with the numeric constant 3.14159. Symbolic constants enable you to create a name for a constant and use the name throughout the program. If the constant needs to be modified throughout the program, it can be modified once in the #define directive. When the program is recompiled, all occurrences of the constant in the program will be modified accordingly.
  • 11. #define: macros A macro is an identifier defined in a #define preprocessor directive. As with symbolic constants, the macro-identifier is replaced in the program with the replacement-text before the program is compiled. Macros may be defined with or without arguments. A macro without arguments is processed like a symbolic constant. In a macro with arguments, the arguments are substituted in the replacement text, then the macro is expanded.
  • 12. #define: macros A symbolic constant is a type of macro. Consider the following macro definition with one argument for the area of a circle: #define CIRCLE_AREA( x ) ( ( PI ) * ( x ) * ( x ) ) Wherever CIRCLE_AREA(y) appears in the file, the value of y is substituted for x in the replacement-text, the symbolic constant PI is replaced by its value (defined previously) and the macro is expanded in the program. The parentheses around each x in the replacement text force the proper order of evaluation when the macro argument is an expression.
  • 13. EXAMPLE •For example, the statement area = CIRCLE_AREA( 4 ); is expanded to area = ( ( 3.14159 ) * ( 4 ) * ( 4 ) ); •For example, the statement area = CIRCLE_AREA( c + 2 ); is expanded to area = (( 3.14159 ) * ( c + 2 ) * ( c + 2 )); which evaluates correctly because the parentheses force the proper order of evaluation.
  • 14. #define: macros If the parentheses are omitted, the macro expansion is area = 3.14159 * c + 2 * c + 2; which evaluates incorrectly as area = ( 3.14159 * c ) + ( 2 * c ) + 2; because of the rules of operator precedence.
  • 15. #define: macros •Symbolic constants and macros can be discarded by using the #undef preprocessor directive. •Directive #undef “undefines” a symbolic constant or macro name. •The scope of a symbolic constant or macro is from its definition until it is undefined with #undef, or until the end of the file. •A macro must be undefined before being redefined to a different value. •Once undefined, a name can be redefined with #define.
  • 16. CONDITIONAL COMPILATION •Conditional compilation enables you to control the execution of preprocessor directives and the compilation of program code. •Each of the conditional preprocessor directives evaluates a constant integer expression. •The conditional preprocessor construct is much like the if selection statement.
  • 17. CONDITIONAL COMPILATION •Consider the following preprocessor code: #if !defined(MY_CONSTANT) #define MY_CONSTANT 0 #endif •These directives determine if MY_CONSTANT is defined. •The expression defined( MY_CONSTANT) evaluates to 1 if MY_CONSTANT is defined; 0 otherwise. •If the result is 0, !defined(MY_CONSTANT) evaluates to 1 and MY_CONSTANT is defined. •Otherwise, the #define directive is skipped.
  • 18. CONDITIONAL COMPILATION •Every #if construct ends with #endif. •Directives #ifdef and #ifndef are shorthand for #if defined and #if !defined. •During program development, it is often helpful to “comment out” portions of code to prevent it from being compiled. •We can use the following preprocessor construct for the same: #if 0 code prevented from compiling #endif •To enable the code to be compiled, replace the 0 in the preceding construct with 1.
  • 19. #error Pre Processor Directives •The #error directive #error tokens prints an implementation-dependent message including the tokens specified in the directive. •The tokens are sequences of characters separated by spaces. •For example, #error 1 - Out of range error •When a #error directive is processed on some systems, the tokens in the directive are displayed as an error message, preprocessing stops and the program does not compile.
  • 20. EXAMPLE #ifdef OS_MSDOS #include <msdos.h> #elifdef OS_UNIX #include "default.h" #else #error Wrong OS!! #endif
  • 21. •The # and ## preprocessor operators are available in Standard C. •The # operator causes a replacement text token to be converted to a string surrounded by quotes. •Example: #define HELLO(x) printf( "Hello, " #x "n" ); •When HELLO(John) appears in a program file, it is expanded to printf( "Hello, " "John" "n" ); •The string "John" replaces #x in the replacement text. # and # # OPERATORS
  • 22. •Strings separated by white space are concatenated during preprocessing, so the preceding statement is equivalent to printf( "Hello, Johnn" ); •The # operator must be used in a macro with arguments because the operand of # refers to an argument of the macro. # and # # OPERATORS
  • 23. •Consider the following macro definition: #define TOKENCONCAT(x, y) x ## y •When TOKENCONCAT appears in the program, its arguments are concatenated and used to replace the macro. •For example, TOKENCONCAT(O, K) is replaced by OK in the program. •The ## operator must have two operands. # and # # OPERATORS
  • 24. •The #line preprocessor directive causes the subsequent source code lines to be renumbered starting with the specified constant integer value. •The directive #line 100 starts line numbering from 100 beginning with the next source code line. •A file name can be included in the #line directive. LINE NUMBERS
  • 26. Want to learn more about programming or Looking to become a good programmer? Are you wasting time on searching so many contents online? Do you want to learn things quickly? Tired of spending huge amount of money to become a Software professional? Do an online course @ baabtra.com We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
  • 27. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 28. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com