SlideShare a Scribd company logo
1 of 46
Download to read offline
C_Programming
Part 6
ENG. KEROLES SHENOUDA
1
C – Preprocessor directives
 Before a C program is compiled in a compiler, source code is processed by a
program called preprocessor. This process is called preprocessing.
 Commands used in preprocessor are called preprocessor directives and they
begin with “#” symbol.
2
A program in C language involves into
different processes
3
DIFFERENCE BETWEEN COMPILERS VS
INTERPRETERS IN C LANGUAGE?
Compilers Interpreters
Compiler reads the entire source code of the
program and converts it into binary
code. This process is called compilation.
Binary code is also referred as machine
code, executable, and object code.
Interpreter reads the program source code
one line at a time and executing that
line. This process is called interpretation.
Program speed is fast. Program speed is slow.
One time execution.
Example: C, C++
Interpretation occurs at every
line of the program.
Example: BASIC
4
list of preprocessor directives that C
programming language offers.
Preprocessor Syntax/Description
Macro
Syntax: #define
This macro defines constant value and can be any of
the basic data types.
Header file inclusion
Syntax: #include <file_name>
The source code of the file “file_name” is included in
the main program at the specified place.
Conditional compilation
Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source
program before compilation with respect to the
condition.
Other directives
Syntax: #undef, #pragma
#undef is used to undefine a defined macro variable.
#Pragma is used to call a function before and after
main function in a C program.
5
Macro 6
Sometimes programmers repeat the writing of the same code segment all over the program.
Sometimes the code segment is repeated exactly another times it repeated with some
changes.
If the repeated code size is large, it is preferable to define a function for this code. If the
repeated code size is small Macros may be the best choice. Macros also do not affect the
execution speed because there is no parameter passing in Macros.
Macro 7
Macros
Is
Text Replacement
Macro
How To see the .i file (precompiled file )
8
First we will
Modify the Project Settings
To Compile not internally
But from external Makefile
Macro
How To see the .i file (precompiled file )
9
Second
Run the Build and Then you will
Find a Makefile will be created
Automatically
Kindly Note You will get full Knowledge
on Makefile on the Next Sessiones
Macro
How To see the .i file (precompiled file )
10
Third: This Makefile
generated
Automatically at
Every time you build
So if we want to
Modify on it without
any change
Do the Following
Unmark it
Macro
How To see the .i file (precompiled file )
11
Fourth : we will add a
commands to generate
.i and .s
By our hand on the
Makefile
Macro
How To see the .i file (precompiled file )
12
Fifth:
Rebuild again
You will see the files
Generated
We can see the precompiled file .i
have a text replacement for MACRO
13
Now are you agree with me
The #define directives define the identifier as a
macro, that is they instruct the compiler to
replace all successive occurrences of
identifier with replacement-list
Another
Example
14
Another
Example
15
__VA_ARGS__
 Version (3) of the #define directive defines a function-like macro with variable
number of arguments. The additional arguments can be accessed using
__VA_ARGS__ identifier, which is then replaced with arguments, supplied with
the identifier to be replaced.
 Version (4) of the #define directive defines a function-like macro with variable
number of arguments, but no regular arguments. The arguments can be
accessed only with __VA_ARGS__ identifier, which is then replaced with
arguments, supplied with identifier to be replaced.
 A ## operator between any two successive identifiers in the replacement-list
runs parameter replacement on the two identifiers and then concatenates the
result
16
17
18
Think in Depth
how to overcome on this error without
remove Dprintf from main
19
Two errors
Think in Depth
how to overcome on this error without
remove Dprintf from main
20
# Convert to string
#undef directive
 The #undef directive undefines the identifier, that is it cancels the previous
definition of the identifier by #define directive. If the identifier does not have
an associated macro, the directive is ignored.
21
EXAMPLE PROGRAM FOR UNDEF IN C
LANGUAGE:
22
Think In Depth
Try to make function factory and use it
by Macro
 Use one macro to create two Functions, one
responsible on multiply input argument to 4
and the other to multiply one input argument
to 2.
 The first function name will be
fun_quadruple(int x)
 The Second one will be fun_double (int x)
23
24
25
Predefined macros
The following macro names are predefined in any translation unit:
26
27
28
Macros ==Text Replacement
Predefined macros
__VA_ARGS__ & # & ##
Conditional compilation
Syntax: #ifdef, #endif, #if,
#else, #ifndef
Set of commands are included or excluded
in source program before compilation with
respect to the condition.
29
EXAMPLE PROGRAM FOR #IFDEF,
#ELSE AND #ENDIF IN C:
 “#ifdef” directive checks whether particular macro is defined or not.
If it is defined, “If” clause statements are included in source file.
 Otherwise, “else” clause statements are included in source file for compilation and execution.
30
EXAMPLE PROGRAM FOR #IFDEF,
#ELSE AND #ENDIF IN C:
31
EXAMPLE PROGRAM FOR #IFDEF,
#ELSE AND #ENDIF IN C:
32
EXAMPLE PROGRAM FOR #IFNDEF AND
#ENDIF IN C:
 #ifndef exactly acts as reverse as #ifdef directive. If particular
macro is not defined, “If” clause statements are included in source file.
 Otherwise, else clause statements are included in source file for compilation
and execution.
33
EXAMPLE PROGRAM FOR #IFNDEF AND
#ENDIF IN C:
34
EXAMPLE PROGRAM FOR #IFNDEF AND
#ENDIF IN C:
35
EXAMPLE PROGRAM FOR #IF, #ELSE
AND #ENDIF IN C:
“If” clause statement is included in source file if given condition is true.
 Otherwise, else clause statement is included in source file for compilation and
execution.
36
37
Pragma directive in c
 Pragma is implementation specific directive i.e each pragma directive has different
implementation rule and use .
 There are many type of pragma directive and varies from one compiler to another
compiler .
 If compiler does not recognize particular pragma the it simply ignore that
pragma statement without showing any error message and execute the
whole program assuming this pragma statement is not present.
38
Pragma
directive in
c
39
We will take
 #pragma startup
 #pragma exit
40
What is #pragma startup and #pragma
exit in c programming language?
 #pragma startup [priority]
 #pragma exit [priority]
 Where priority is optional integral number.
 For user priority varies from 64 to 255
 For c libraries priority varies from 0 to 63
 Default priority is 100.
41
What is #pragma startup and #pragma
exit in c programming language?
 pragma startup always execute the function before the main function
pragma exit always execute the function after the main function.Function
declaration of must be before startup and exit pragma directives and function must
not take any argument and return void. If more than one startup directive then
priority decides which will execute first.
 startup:
 Lower value: higher priority i.e. functions will execute first. If more than one exit directive
then priority decides which will execute first.
 exit:
 Higher value: higher priority i.e. functions will execute first. For example
42
43
#pragma
startup/exit ...
is not
supported in
gcc.
 In gcc and clang, you should use
__attribute__((constructor)) and
__attribute__((destructor))
instead.
44
45
Quiz: Think how to write a
code can be
Removed or added to the
Embedded C Program and
Responsible on print Debug
with Details
References 46
 The Case for Learning C as Your First Programming Language
 A Tutorial on Data Representation
 std::printf, std::fprintf, std::sprintf, std::snprintf…..
 C Programming for Engineers, Dr. Mohamed Sobh
 C programming expert.
 fresh2refresh.com/c-programming
 C programming Interview questions and answers
 C – Preprocessor directives

More Related Content

What's hot (20)

Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Misra c
Misra cMisra c
Misra c
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Embedded C - Lecture 3
Embedded C - Lecture 3Embedded C - Lecture 3
Embedded C - Lecture 3
 
En.dm00024550 stm8 s003f3p6
En.dm00024550   stm8 s003f3p6En.dm00024550   stm8 s003f3p6
En.dm00024550 stm8 s003f3p6
 
An Introduction to MISRA C:2012
An Introduction to MISRA C:2012An Introduction to MISRA C:2012
An Introduction to MISRA C:2012
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
 
Misra c rules
Misra c rulesMisra c rules
Misra c rules
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
 
PCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-ReviewPCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-Review
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Automative basics v3
Automative basics v3Automative basics v3
Automative basics v3
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 

Viewers also liked (20)

C programming part2
C programming part2C programming part2
C programming part2
 
Microcontroller part 7_v1
Microcontroller part 7_v1Microcontroller part 7_v1
Microcontroller part 7_v1
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Microcontroller part 9_v1
Microcontroller part 9_v1Microcontroller part 9_v1
Microcontroller part 9_v1
 
Microcontroller part 8_v1
Microcontroller part 8_v1Microcontroller part 8_v1
Microcontroller part 8_v1
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
Microcontroller part 6_v1
Microcontroller part 6_v1Microcontroller part 6_v1
Microcontroller part 6_v1
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Homework 2
Homework 2Homework 2
Homework 2
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
C programming part4
C programming part4C programming part4
C programming part4
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 

Similar to C programming session6

Similar to C programming session6 (20)

Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
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
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
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
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
Preprocesser in c
Preprocesser in cPreprocesser in c
Preprocesser in c
 
Preprocessor.pptx
Preprocessor.pptxPreprocessor.pptx
Preprocessor.pptx
 
pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Preprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishPreprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danish
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 

More from Keroles karam khalil (18)

C basics quiz part 1_solution
C basics quiz part 1_solutionC basics quiz part 1_solution
C basics quiz part 1_solution
 
Autosar Basics hand book_v1
Autosar Basics  hand book_v1Autosar Basics  hand book_v1
Autosar Basics hand book_v1
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Quiz 10
Quiz 10Quiz 10
Quiz 10
 
Homework 6
Homework 6Homework 6
Homework 6
 
Homework 5 solution
Homework 5 solutionHomework 5 solution
Homework 5 solution
 
Notes part7
Notes part7Notes part7
Notes part7
 
Homework 5
Homework 5Homework 5
Homework 5
 
Notes part6
Notes part6Notes part6
Notes part6
 
Homework 4 solution
Homework 4 solutionHomework 4 solution
Homework 4 solution
 
Notes part5
Notes part5Notes part5
Notes part5
 
Homework 4
Homework 4Homework 4
Homework 4
 
Homework 3 solution
Homework 3 solutionHomework 3 solution
Homework 3 solution
 
C programming session5
C programming  session5C programming  session5
C programming session5
 
Session 5-exersice
Session 5-exersiceSession 5-exersice
Session 5-exersice
 

Recently uploaded

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

C programming session6

  • 2. C – Preprocessor directives  Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.  Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol. 2
  • 3. A program in C language involves into different processes 3
  • 4. DIFFERENCE BETWEEN COMPILERS VS INTERPRETERS IN C LANGUAGE? Compilers Interpreters Compiler reads the entire source code of the program and converts it into binary code. This process is called compilation. Binary code is also referred as machine code, executable, and object code. Interpreter reads the program source code one line at a time and executing that line. This process is called interpretation. Program speed is fast. Program speed is slow. One time execution. Example: C, C++ Interpretation occurs at every line of the program. Example: BASIC 4
  • 5. list of preprocessor directives that C programming language offers. Preprocessor Syntax/Description Macro Syntax: #define This macro defines constant value and can be any of the basic data types. Header file inclusion Syntax: #include <file_name> The source code of the file “file_name” is included in the main program at the specified place. Conditional compilation Syntax: #ifdef, #endif, #if, #else, #ifndef Set of commands are included or excluded in source program before compilation with respect to the condition. Other directives Syntax: #undef, #pragma #undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program. 5
  • 6. Macro 6 Sometimes programmers repeat the writing of the same code segment all over the program. Sometimes the code segment is repeated exactly another times it repeated with some changes. If the repeated code size is large, it is preferable to define a function for this code. If the repeated code size is small Macros may be the best choice. Macros also do not affect the execution speed because there is no parameter passing in Macros.
  • 8. Macro How To see the .i file (precompiled file ) 8 First we will Modify the Project Settings To Compile not internally But from external Makefile
  • 9. Macro How To see the .i file (precompiled file ) 9 Second Run the Build and Then you will Find a Makefile will be created Automatically Kindly Note You will get full Knowledge on Makefile on the Next Sessiones
  • 10. Macro How To see the .i file (precompiled file ) 10 Third: This Makefile generated Automatically at Every time you build So if we want to Modify on it without any change Do the Following Unmark it
  • 11. Macro How To see the .i file (precompiled file ) 11 Fourth : we will add a commands to generate .i and .s By our hand on the Makefile
  • 12. Macro How To see the .i file (precompiled file ) 12 Fifth: Rebuild again You will see the files Generated
  • 13. We can see the precompiled file .i have a text replacement for MACRO 13 Now are you agree with me The #define directives define the identifier as a macro, that is they instruct the compiler to replace all successive occurrences of identifier with replacement-list
  • 16. __VA_ARGS__  Version (3) of the #define directive defines a function-like macro with variable number of arguments. The additional arguments can be accessed using __VA_ARGS__ identifier, which is then replaced with arguments, supplied with the identifier to be replaced.  Version (4) of the #define directive defines a function-like macro with variable number of arguments, but no regular arguments. The arguments can be accessed only with __VA_ARGS__ identifier, which is then replaced with arguments, supplied with identifier to be replaced.  A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers and then concatenates the result 16
  • 17. 17
  • 18. 18
  • 19. Think in Depth how to overcome on this error without remove Dprintf from main 19 Two errors
  • 20. Think in Depth how to overcome on this error without remove Dprintf from main 20 # Convert to string
  • 21. #undef directive  The #undef directive undefines the identifier, that is it cancels the previous definition of the identifier by #define directive. If the identifier does not have an associated macro, the directive is ignored. 21
  • 22. EXAMPLE PROGRAM FOR UNDEF IN C LANGUAGE: 22
  • 23. Think In Depth Try to make function factory and use it by Macro  Use one macro to create two Functions, one responsible on multiply input argument to 4 and the other to multiply one input argument to 2.  The first function name will be fun_quadruple(int x)  The Second one will be fun_double (int x) 23
  • 24. 24
  • 25. 25
  • 26. Predefined macros The following macro names are predefined in any translation unit: 26
  • 27. 27
  • 28. 28 Macros ==Text Replacement Predefined macros __VA_ARGS__ & # & ##
  • 29. Conditional compilation Syntax: #ifdef, #endif, #if, #else, #ifndef Set of commands are included or excluded in source program before compilation with respect to the condition. 29
  • 30. EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C:  “#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If” clause statements are included in source file.  Otherwise, “else” clause statements are included in source file for compilation and execution. 30
  • 31. EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C: 31
  • 32. EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C: 32
  • 33. EXAMPLE PROGRAM FOR #IFNDEF AND #ENDIF IN C:  #ifndef exactly acts as reverse as #ifdef directive. If particular macro is not defined, “If” clause statements are included in source file.  Otherwise, else clause statements are included in source file for compilation and execution. 33
  • 34. EXAMPLE PROGRAM FOR #IFNDEF AND #ENDIF IN C: 34
  • 35. EXAMPLE PROGRAM FOR #IFNDEF AND #ENDIF IN C: 35
  • 36. EXAMPLE PROGRAM FOR #IF, #ELSE AND #ENDIF IN C: “If” clause statement is included in source file if given condition is true.  Otherwise, else clause statement is included in source file for compilation and execution. 36
  • 37. 37
  • 38. Pragma directive in c  Pragma is implementation specific directive i.e each pragma directive has different implementation rule and use .  There are many type of pragma directive and varies from one compiler to another compiler .  If compiler does not recognize particular pragma the it simply ignore that pragma statement without showing any error message and execute the whole program assuming this pragma statement is not present. 38
  • 40. We will take  #pragma startup  #pragma exit 40
  • 41. What is #pragma startup and #pragma exit in c programming language?  #pragma startup [priority]  #pragma exit [priority]  Where priority is optional integral number.  For user priority varies from 64 to 255  For c libraries priority varies from 0 to 63  Default priority is 100. 41
  • 42. What is #pragma startup and #pragma exit in c programming language?  pragma startup always execute the function before the main function pragma exit always execute the function after the main function.Function declaration of must be before startup and exit pragma directives and function must not take any argument and return void. If more than one startup directive then priority decides which will execute first.  startup:  Lower value: higher priority i.e. functions will execute first. If more than one exit directive then priority decides which will execute first.  exit:  Higher value: higher priority i.e. functions will execute first. For example 42
  • 43. 43
  • 44. #pragma startup/exit ... is not supported in gcc.  In gcc and clang, you should use __attribute__((constructor)) and __attribute__((destructor)) instead. 44
  • 45. 45 Quiz: Think how to write a code can be Removed or added to the Embedded C Program and Responsible on print Debug with Details
  • 46. References 46  The Case for Learning C as Your First Programming Language  A Tutorial on Data Representation  std::printf, std::fprintf, std::sprintf, std::snprintf…..  C Programming for Engineers, Dr. Mohamed Sobh  C programming expert.  fresh2refresh.com/c-programming  C programming Interview questions and answers  C – Preprocessor directives