SlideShare a Scribd company logo
1 of 18
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

file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 

What's hot (20)

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
Function in cFunction in c
Function in c
 
file handling c++
file handling c++file handling c++
file handling c++
 
C functions
C functionsC functions
C functions
 
Friend functions
Friend functions Friend functions
Friend functions
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++
C++C++
C++
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 

Similar to Lecture 21 - Preprocessor and Header File

Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
ssuserf86fba
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
alish sha
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-framework
Nilesh Bangar
 

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

C language header files
C language header filesC language header files
C language header files
 
cppProgramStructure.ppt
cppProgramStructure.pptcppProgramStructure.ppt
cppProgramStructure.ppt
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
 
Lecture 3 in c ppt
Lecture 3 in c pptLecture 3 in c ppt
Lecture 3 in c ppt
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
google sites coding & programming language.pptx
google sites coding & programming language.pptxgoogle sites coding & programming language.pptx
google sites coding & programming language.pptx
 
Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programming
 
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
 
Preprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danishPreprocesser in c++ by thanveer danish
Preprocesser in c++ by thanveer danish
 
live journal coding & programming language.pdf
live journal coding & programming language.pdflive journal coding & programming language.pdf
live journal coding & programming language.pdf
 
Whats a header- Whats a source-code file- Discuss the purpose of each-.docx
Whats a header- Whats a source-code file- Discuss the purpose of each-.docxWhats a header- Whats a source-code file- Discuss the purpose of each-.docx
Whats a header- Whats a source-code file- Discuss the purpose of each-.docx
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
blogger coding & programming language.pptx
blogger coding & programming language.pptxblogger coding & programming language.pptx
blogger coding & programming language.pptx
 
blogger coding & programming language.pdf
blogger coding & programming language.pdfblogger coding & programming language.pdf
blogger coding & programming language.pdf
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-framework
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 

More from Md. Imran Hossain Showrov

More from Md. Imran Hossain Showrov (20)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 

Recently uploaded

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 

Lecture 21 - Preprocessor and Header File

  • 1. The Preprocessor and Header File Md. Imran Hossain Showrov (showrovsworld@gmail.com) 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>