SlideShare una empresa de Scribd logo
1 de 10
Preprocessor

The C preprocessor executes before a program
is compiled.

Some actions it performs are the inclusion of
other files in the file being compiled, definition
of symbolic constants and macros, conditional
compilation of program code and conditional
execution of preprocessor directives.

Preprocessor directives begin with # and only
white-space characters and comments may
appear before a preprocessor directive on a
line.
Preprocessor
Preprocessor directive
a C program line beginning with # that provides
an instruction to the preprocessor
Preprocessor
a system program that modifies a C program prior
to its compilation
Library
a collection of useful functions and symbols that
may be accessed by a program
The #include Preprocessor Directive
#include
copy of a specified file included in place of the directive
#include <filename> - searches standard library for file (use for
standard library files)
#include "filename" - searches current directory, then
standard library (use for user-defined files)
Used for
loading header files (#include <iostream>)
programs with multiple source files to be compiled together
header file - has common declarations and definitions (classes, structures,
function prototypes)
#include statement in each file
The #define Preprocessor Directive: Symbolic
Constants
#define
preprocessor directive used to create symbolic constants and macros.
Symbolic constants
when program compiled, all occurrences of symbolic constant replaced with
replacement text
Format: #define identifier replacement-text
Example: #define PI 3.14159
everything to right of identifier replaces text
#define PI = 3.14159
replaces "PI" with " = 3.14159", probably results in an error
cannot redefine symbolic constants with more #define statements
The #define Preprocessor Directive: Macros
Macro - operation defined in #define
intended for C programs
macro without arguments: treated like a symbolic constant
macro with arguments: arguments substituted for replacement text, macro
expanded
performs a text substitution - no data type checking
Example:
#define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
area = CIRCLE_AREA( 4 );
becomes
area = ( 3.14159 * ( 4 ) * ( 4 ) );
Conditional Compilation
conditional compilation
control preprocessor directives and compilation
cast expressions, sizeof, enumeration constants cannot be evaluated
structure similar to if
#if !defined( NULL )
#define NULL 0
#endif
determines if symbolic constant NULL defined
if NULL is defined, defined(NULL) evaluates to 1
if NULL not defined, defines NULL as 0
every #if ends with #endif
#ifdef short for #if defined(name)
#ifndef short for #if !defined(name)
The #error and #pragma Preprocessor Directives
#error tokens
tokens - sequences of characters separated by spaces
"I like C" has 3 tokens
prints message and tokens (depends on implementation)
for example: when #error encountered, tokens displayed and preprocessing
stops (program does not compile)
#pragma tokens
implementation defined action (consult compiler documentation)
pragmas not recognized by compiler are ignored
Line Numbers
#line
renumbers subsequent code lines, starting with integer value
file name can be included
#line 100 "myFile.c"
lines are numbered from 100 beginning with next source code file
for purposes of errors, file name is "myFile.c"
makes errors more meaningful
line numbers do not appear in source file
Predefined Symbolic Constants
Five predefined symbolic constants
cannot be used in #define or #undef
Symbolic
constant
Description
__LINE__ The line number of the current source code line (an integer
constant).
__FILE__ The presumed name of the source file (a string).
__DATE__ The date the source file is compiled (a string of the form
"Mmm dd yyyy" such as "Jan 19 2001").
__TIME__ The time the source file is compiled (a string literal of the
form "hh:mm:ss").
Assertions
assert macro
header <cassert>
tests value of an expression
if 0 (false) prints error message and calls abort
assert( x <= 10 );
if NDEBUG defined, all subsequent assert statements
ignored
#define NDEBUG

Más contenido relacionado

La actualidad más candente (20)

Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Advanced C - Part 1
Advanced C - Part 1 Advanced C - Part 1
Advanced C - Part 1
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Basic Structure of C Language and Related Term
Basic Structure of C Language  and Related TermBasic Structure of C Language  and Related Term
Basic Structure of C Language and Related Term
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
Embedded C
Embedded CEmbedded C
Embedded C
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
File in C language
File in C languageFile in C language
File in C language
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
Function in C
Function in CFunction in C
Function in C
 
C functions
C functionsC functions
C functions
 

Destacado

The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessoriuui
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CTushar B Kute
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in CArpana shree
 
Operatingsystems lecture2
Operatingsystems lecture2Operatingsystems lecture2
Operatingsystems lecture2Gaurav Meena
 
Sexuality education
Sexuality educationSexuality education
Sexuality educationTarek Anis
 
Chapter 2: The Recording Process
Chapter 2: The Recording ProcessChapter 2: The Recording Process
Chapter 2: The Recording Processtepsokha
 
Overview of c language
Overview of c languageOverview of c language
Overview of c languageshalini392
 
Accounting principles weygandt 11th edition solutions manual
Accounting principles weygandt 11th edition solutions manual Accounting principles weygandt 11th edition solutions manual
Accounting principles weygandt 11th edition solutions manual abb183n
 
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn LinkedIn
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
 
The Atomic Guide to Content Strategy
The Atomic Guide to Content StrategyThe Atomic Guide to Content Strategy
The Atomic Guide to Content Strategyfrankdelmelle
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 
African Americans: College Majors and Earnings
African Americans: College Majors and Earnings African Americans: College Majors and Earnings
African Americans: College Majors and Earnings CEW Georgetown
 

Destacado (20)

The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Operatingsystems lecture2
Operatingsystems lecture2Operatingsystems lecture2
Operatingsystems lecture2
 
Sexuality education
Sexuality educationSexuality education
Sexuality education
 
Chapter 2: The Recording Process
Chapter 2: The Recording ProcessChapter 2: The Recording Process
Chapter 2: The Recording Process
 
Sex education
Sex educationSex education
Sex education
 
Overview of c language
Overview of c languageOverview of c language
Overview of c language
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Ch2
Ch2Ch2
Ch2
 
Accounting principles weygandt 11th edition solutions manual
Accounting principles weygandt 11th edition solutions manual Accounting principles weygandt 11th edition solutions manual
Accounting principles weygandt 11th edition solutions manual
 
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn
Inspiration for Marketers: 10 Inspiring Posts from Influencers on LinkedIn
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
The Atomic Guide to Content Strategy
The Atomic Guide to Content StrategyThe Atomic Guide to Content Strategy
The Atomic Guide to Content Strategy
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
C language ppt
C language pptC language ppt
C language ppt
 
Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
African Americans: College Majors and Earnings
African Americans: College Majors and Earnings African Americans: College Majors and Earnings
African Americans: College Majors and Earnings
 

Similar a Preprocessors

1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptxAlAmos4
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11patcha535
 
Preprocessor
PreprocessorPreprocessor
PreprocessorVõ Hòa
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assemblyAbdul Khan
 

Similar a Preprocessors (20)

ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
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
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
C programming session6
C programming  session6C programming  session6
C programming session6
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Chapter3
Chapter3Chapter3
Chapter3
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assembly
 

Último

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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
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
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
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
 
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
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
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
 

Último (20)

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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
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.
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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
 
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
 
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...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
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
 

Preprocessors

  • 1. Preprocessor  The C preprocessor executes before a program is compiled.  Some actions it performs are the inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives.  Preprocessor directives begin with # and only white-space characters and comments may appear before a preprocessor directive on a line.
  • 2. Preprocessor Preprocessor directive a C program line beginning with # that provides an instruction to the preprocessor Preprocessor a system program that modifies a C program prior to its compilation Library a collection of useful functions and symbols that may be accessed by a program
  • 3. The #include Preprocessor Directive #include copy of a specified file included in place of the directive #include <filename> - searches standard library for file (use for standard library files) #include "filename" - searches current directory, then standard library (use for user-defined files) Used for loading header files (#include <iostream>) programs with multiple source files to be compiled together header file - has common declarations and definitions (classes, structures, function prototypes) #include statement in each file
  • 4. The #define Preprocessor Directive: Symbolic Constants #define preprocessor directive used to create symbolic constants and macros. Symbolic constants when program compiled, all occurrences of symbolic constant replaced with replacement text Format: #define identifier replacement-text Example: #define PI 3.14159 everything to right of identifier replaces text #define PI = 3.14159 replaces "PI" with " = 3.14159", probably results in an error cannot redefine symbolic constants with more #define statements
  • 5. The #define Preprocessor Directive: Macros Macro - operation defined in #define intended for C programs macro without arguments: treated like a symbolic constant macro with arguments: arguments substituted for replacement text, macro expanded performs a text substitution - no data type checking Example: #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) area = CIRCLE_AREA( 4 ); becomes area = ( 3.14159 * ( 4 ) * ( 4 ) );
  • 6. Conditional Compilation conditional compilation control preprocessor directives and compilation cast expressions, sizeof, enumeration constants cannot be evaluated structure similar to if #if !defined( NULL ) #define NULL 0 #endif determines if symbolic constant NULL defined if NULL is defined, defined(NULL) evaluates to 1 if NULL not defined, defines NULL as 0 every #if ends with #endif #ifdef short for #if defined(name) #ifndef short for #if !defined(name)
  • 7. The #error and #pragma Preprocessor Directives #error tokens tokens - sequences of characters separated by spaces "I like C" has 3 tokens prints message and tokens (depends on implementation) for example: when #error encountered, tokens displayed and preprocessing stops (program does not compile) #pragma tokens implementation defined action (consult compiler documentation) pragmas not recognized by compiler are ignored
  • 8. Line Numbers #line renumbers subsequent code lines, starting with integer value file name can be included #line 100 "myFile.c" lines are numbered from 100 beginning with next source code file for purposes of errors, file name is "myFile.c" makes errors more meaningful line numbers do not appear in source file
  • 9. Predefined Symbolic Constants Five predefined symbolic constants cannot be used in #define or #undef Symbolic constant Description __LINE__ The line number of the current source code line (an integer constant). __FILE__ The presumed name of the source file (a string). __DATE__ The date the source file is compiled (a string of the form "Mmm dd yyyy" such as "Jan 19 2001"). __TIME__ The time the source file is compiled (a string literal of the form "hh:mm:ss").
  • 10. Assertions assert macro header <cassert> tests value of an expression if 0 (false) prints error message and calls abort assert( x <= 10 ); if NDEBUG defined, all subsequent assert statements ignored #define NDEBUG