SlideShare a Scribd company logo
3
Most read
11
Most read
12
Most read
The Preprocessor and Header File
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
21
1
Outline
 What is Preprocessor?
 Preprocessor Directives
 Preprocessor Directives Categories
 Macro Expansion
 File Inclusion
 Conditional compilation
 What is a Header File?
 Why Header Files are used?
What is Preprocessor?
 The preprocessor is a program that processes the
source code before it passes through the compiler.
 Produce a source code file with the preprocessing
commands properly sorted out.
Preprocessor Directives
 Preprocessor commands are known as directives.
 Preprocessor provides certain features.
 These features are also known as preprocessor
directives.
 Preprocessor directives start with # sign.
#include <stdio.h>
Preprocessor Directives (cont..)
 Preprocessor directives can be placed any where in
the source program.
 Note: Place it at start of the program.
 Each preprocessor directive must be on it’s own line.
Preprocessor Directives (cont..)
Preprocessor Directives Categories
 Preprocessor directives can be divided into three
categories:
1. Macro Expansion
2. File Inclusion
3. Conditional compilation (also known as compiler
control directives)
1. Macro Expansion
 #define directive is known as macro expansion.
 Definition:
 General Form:
#define macro_template macro_expansion
#define macro_name char_sequence
1. Macro Expansion (cont..)
 Preprocessor search for macro definition.
 After finding #define directive it search entire
program for macro_template.
 Replace each macro_template with
macro_expansion.
 Best Practice: Use capital letters for macro
template.
 Do not use semicolon ‘ ; ’
Why Macro Expansion is used?
 To write efficient programs.
 To increase readiabiality of programs.
 Variable vs macro_template
 Compiler can generate faster and compact code for
constant than it can for variables.
 When you are dealing with a constant, why use
variable.
 A variable may change in the program.
2. File Inclusion
 causes one file to be included in another.
 <filename> : search the directory on current
directory only.
 “filename” : search the directory on current directory
and specified directories as specified in the include
search path.
Why File Inclusion is used?
 Divide a program in multiple files.
 Each file contains related functions.
 Some functions or macros are required in each
program
 Put them in a file (Library).
 Include them in program that need them.
 Nested includes: Included file may have more included
files in it.
3. Conditional Compilation/ Compiler
Control Directives
 Write single program to run on different
environments.
 #ifdef – if defined
 #endif – end if
 #else – else
 #ifndef – in not defined
 #if – if
 #elif – else if
Where Conditional Compilation is used?
 To avoide multiple declaration error
 Instead of comments(Nested comments not allowed)
 Run the same code on different environment.
What is a Header File?
 A header file is a file with extension .h which contains
C function declarations and macro definitions to be
shared between several source files.
 There are two types of header files:
1. The files that the programmer writes and
2. The files that comes with your compiler.
Why Header Files are used?
 Including a header file is equal to copying the content
of the header file but we do not do it because it will
be error-prone and it is not a good idea to copy the
content of a header file in the source files, especially if
we have multiple source files in a program.
The include syntax
 Both the user and the system header files are included
using the preprocessing directive #include. It has the
following two forms −
 #include <file>
 #include “file”
 Example:
 #include<stdio.h>
 #include<string.h>
Lecture 21 - Preprocessor and Header File

More Related Content

What's hot (20)

PPTX
Relational Algebra,Types of join
raj upadhyay
 
PPTX
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
PPTX
Types of Compilers
Hemant Chetwani
 
PPTX
joins in dbms its describes about how joins are important and necessity in d...
AshokRachapalli1
 
PPSX
computer language - Html frames
Dr. I. Uma Maheswari Maheswari
 
PDF
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PPTX
Procedural programming
Ankit92Chitnavis
 
PPT
Transactions in dbms
Nancy Gulati
 
PPT
Files Vs DataBase
Dr. C.V. Suresh Babu
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
Semantic Analysis.pptx
ZarfaMasood
 
PPTX
Demand Paging in OS (Operating System): Example, Advantages, Working
DigitalThinkerHelp
 
PPTX
Programming Paradigm & Languages
Gaditek
 
PPT
Basics of c++
Huba Akhtar
 
PPT
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Oum Saokosal
 
PPT
Servlet life cycle
Venkateswara Rao N
 
PDF
Normalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NF
Biplap Bhattarai
 
PDF
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
PPT
7.data types in c#
Zeeshan Ahmad
 
PPTX
Segmentation in operating systems
Dr. Jasmine Beulah Gnanadurai
 
Relational Algebra,Types of join
raj upadhyay
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
Types of Compilers
Hemant Chetwani
 
joins in dbms its describes about how joins are important and necessity in d...
AshokRachapalli1
 
computer language - Html frames
Dr. I. Uma Maheswari Maheswari
 
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Procedural programming
Ankit92Chitnavis
 
Transactions in dbms
Nancy Gulati
 
Files Vs DataBase
Dr. C.V. Suresh Babu
 
Operators and expressions in c language
tanmaymodi4
 
Semantic Analysis.pptx
ZarfaMasood
 
Demand Paging in OS (Operating System): Example, Advantages, Working
DigitalThinkerHelp
 
Programming Paradigm & Languages
Gaditek
 
Basics of c++
Huba Akhtar
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Oum Saokosal
 
Servlet life cycle
Venkateswara Rao N
 
Normalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NF
Biplap Bhattarai
 
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
7.data types in c#
Zeeshan Ahmad
 
Segmentation in operating systems
Dr. Jasmine Beulah Gnanadurai
 

Similar to Lecture 21 - Preprocessor and Header File (20)

PDF
6 preprocessor macro header
hasan Mohammad
 
PPTX
Preprocessor
lalithambiga kamaraj
 
PPTX
UNIT 4A-preprocessor.pptx for c language and basic knowledge
2024163103shubham
 
PPT
Preprocessors
Koganti Ravikumar
 
PPTX
1 - Preprocessor.pptx
AlAmos4
 
PDF
Preprocessor
Learn By Watch
 
PDF
ANSI C Macros
Srikrishnan Suresh
 
PPTX
Preprocessor directives in c language
tanmaymodi4
 
PPTX
Preprocessor directives in c laguage
Tanmay Modi
 
PPT
PreProcessorDirective.ppt
Osmania University
 
PPTX
introduction of c langauge(I unit)
Prashant Sharma
 
PPTX
Pre processor directives in c
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
U get to UnderstandingBasicStructureC.pptx
devi96742
 
PDF
Module 05 Preprocessor and Macros in C
Tushar B Kute
 
PPT
Unit 5 cspc
BBDITM LUCKNOW
 
PDF
Introduction to Preprocessors
Thesis Scientist Private Limited
 
PPTX
Preprocessor
Võ Hòa
 
PPT
Preprocessors
Gourav Arora
 
PPTX
Programming Fundamentals lecture 5
REHAN IJAZ
 
6 preprocessor macro header
hasan Mohammad
 
Preprocessor
lalithambiga kamaraj
 
UNIT 4A-preprocessor.pptx for c language and basic knowledge
2024163103shubham
 
Preprocessors
Koganti Ravikumar
 
1 - Preprocessor.pptx
AlAmos4
 
Preprocessor
Learn By Watch
 
ANSI C Macros
Srikrishnan Suresh
 
Preprocessor directives in c language
tanmaymodi4
 
Preprocessor directives in c laguage
Tanmay Modi
 
PreProcessorDirective.ppt
Osmania University
 
introduction of c langauge(I unit)
Prashant Sharma
 
U get to UnderstandingBasicStructureC.pptx
devi96742
 
Module 05 Preprocessor and Macros in C
Tushar B Kute
 
Unit 5 cspc
BBDITM LUCKNOW
 
Introduction to Preprocessors
Thesis Scientist Private Limited
 
Preprocessor
Võ Hòa
 
Preprocessors
Gourav Arora
 
Programming Fundamentals lecture 5
REHAN IJAZ
 
Ad

More from Md. Imran Hossain Showrov (20)

PPT
Lecture 22 - Error Handling
Md. Imran Hossain Showrov
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PPT
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
PPT
Lecture 16 - Multi dimensional Array
Md. Imran Hossain Showrov
 
PPT
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
PPT
Lecture 15 - Array
Md. Imran Hossain Showrov
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PPT
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
PPT
Lecture 12 - Recursion
Md. Imran Hossain Showrov
 
PPT
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
PPT
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
PPT
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
PPT
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
PPT
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
PPT
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
PPT
Lecture 4- Computer Software and Languages
Md. Imran Hossain Showrov
 
PPT
Lecture 3 - Processors, Memory and I/O devices
Md. Imran Hossain Showrov
 
PPT
Lecture 2 - Introductory Concepts
Md. Imran Hossain Showrov
 
PPT
Lecture 1- History of C Programming
Md. Imran Hossain Showrov
 
Lecture 22 - Error Handling
Md. Imran Hossain Showrov
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Lecture 16 - Multi dimensional Array
Md. Imran Hossain Showrov
 
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
Lecture 15 - Array
Md. Imran Hossain Showrov
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Lecture 12 - Recursion
Md. Imran Hossain Showrov
 
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Lecture 4- Computer Software and Languages
Md. Imran Hossain Showrov
 
Lecture 3 - Processors, Memory and I/O devices
Md. Imran Hossain Showrov
 
Lecture 2 - Introductory Concepts
Md. Imran Hossain Showrov
 
Lecture 1- History of C Programming
Md. Imran Hossain Showrov
 
Ad

Recently uploaded (20)

PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
GENERAL BIOLOGY 1 - Subject Introduction
marvinnbustamante1
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
Introduction presentation of the patentbutler tool
MIPLM
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
GENERAL BIOLOGY 1 - Subject Introduction
marvinnbustamante1
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
Different types of inheritance in odoo 18
Celine George
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 

Lecture 21 - Preprocessor and Header File

  • 1. The Preprocessor and Header File Md. Imran Hossain Showrov ([email protected]) 21 1
  • 2. Outline  What is Preprocessor?  Preprocessor Directives  Preprocessor Directives Categories  Macro Expansion  File Inclusion  Conditional compilation  What is a Header File?  Why Header Files are used?
  • 3. What is Preprocessor?  The preprocessor is a program that processes the source code before it passes through the compiler.  Produce a source code file with the preprocessing commands properly sorted out.
  • 4. Preprocessor Directives  Preprocessor commands are known as directives.  Preprocessor provides certain features.  These features are also known as preprocessor directives.  Preprocessor directives start with # sign. #include <stdio.h>
  • 5. Preprocessor Directives (cont..)  Preprocessor directives can be placed any where in the source program.  Note: Place it at start of the program.  Each preprocessor directive must be on it’s own line.
  • 7. Preprocessor Directives Categories  Preprocessor directives can be divided into three categories: 1. Macro Expansion 2. File Inclusion 3. Conditional compilation (also known as compiler control directives)
  • 8. 1. Macro Expansion  #define directive is known as macro expansion.  Definition:  General Form: #define macro_template macro_expansion #define macro_name char_sequence
  • 9. 1. Macro Expansion (cont..)  Preprocessor search for macro definition.  After finding #define directive it search entire program for macro_template.  Replace each macro_template with macro_expansion.  Best Practice: Use capital letters for macro template.  Do not use semicolon ‘ ; ’
  • 10. Why Macro Expansion is used?  To write efficient programs.  To increase readiabiality of programs.  Variable vs macro_template  Compiler can generate faster and compact code for constant than it can for variables.  When you are dealing with a constant, why use variable.  A variable may change in the program.
  • 11. 2. File Inclusion  causes one file to be included in another.  <filename> : search the directory on current directory only.  “filename” : search the directory on current directory and specified directories as specified in the include search path.
  • 12. Why File Inclusion is used?  Divide a program in multiple files.  Each file contains related functions.  Some functions or macros are required in each program  Put them in a file (Library).  Include them in program that need them.  Nested includes: Included file may have more included files in it.
  • 13. 3. Conditional Compilation/ Compiler Control Directives  Write single program to run on different environments.  #ifdef – if defined  #endif – end if  #else – else  #ifndef – in not defined  #if – if  #elif – else if
  • 14. Where Conditional Compilation is used?  To avoide multiple declaration error  Instead of comments(Nested comments not allowed)  Run the same code on different environment.
  • 15. What is a Header File?  A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.  There are two types of header files: 1. The files that the programmer writes and 2. The files that comes with your compiler.
  • 16. Why Header Files are used?  Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.
  • 17. The include syntax  Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms −  #include <file>  #include “file”  Example:  #include<stdio.h>  #include<string.h>