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 (20)

C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
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
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
algo
algoalgo
algo
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
Programming in c
Programming in cProgramming in c
Programming in c
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++
C++C++
C++
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
A c program of Phonebook application
A c program of Phonebook applicationA c program of Phonebook application
A c program of Phonebook application
 
C notes
C notesC notes
C notes
 
Chapter3
Chapter3Chapter3
Chapter3
 

Similar to Lecture 21 - Preprocessor and Header File

C language header files
C language header filesC language header files
C language header filesmarar hina
 
cppProgramStructure.ppt
cppProgramStructure.pptcppProgramStructure.ppt
cppProgramStructure.pptDaveCalapis4
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
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 conceptsssuserf86fba
 
Lecture 3 in c ppt
Lecture 3 in c pptLecture 3 in c ppt
Lecture 3 in c pptumair ansari
 
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 dti2143alish sha
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
google sites coding & programming language.pptx
google sites coding & programming language.pptxgoogle sites coding & programming language.pptx
google sites coding & programming language.pptxdevbhargav1
 
Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programmingsonalikharade3
 
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
 
live journal coding & programming language.pdf
live journal coding & programming language.pdflive journal coding & programming language.pdf
live journal coding & programming language.pdfdevbhargav1
 
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-.docxearleanp
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
blogger coding & programming language.pptx
blogger coding & programming language.pptxblogger coding & programming language.pptx
blogger coding & programming language.pptxdevbhargav1
 
blogger coding & programming language.pdf
blogger coding & programming language.pdfblogger coding & programming language.pdf
blogger coding & programming language.pdfdevbhargav1
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-frameworkNilesh Bangar
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 

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

Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Md. Imran Hossain Showrov
 
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 devicesMd. 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

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

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>