SlideShare a Scribd company logo
The C Preprocessor
Yongjoon Lee
April 22, 2005
What is it?
• Things with #
– #include <stdio.h>
• Processes the C source files
BEFORE handing it to compiler.
– `Pre`-process
– gcc –E
– cpp
Why care?
• File Inclusion
• Basic Macros
• Parameterized Macros
• Conditional Compilation
• Style and (mis)use of Macros
File Inclusion
• Recall : #include <filename>
– #include <foo.h>
• System directories
– #include “foo.h”
• Current directories
– gcc -I
Macros
• We can #define constants
#define HALF_PI 1.5708
#define BAD_PI HALF_PI+HALF_PI
• Solution?
((((((((((The parentheses!))))))))))
#define PI ((HALF_PI)+(HALF_PI))
Parameterized Macros
• Macros with param :
#define BAAAD_DOUBLE(x) (x*2)
#define BAD_DOUBLE(x) ((x)+(x))
#define OKAY_DOUBLE(x) ((x)*2)
• WHAT TO TAKE HOME:
– Don’t use 2+ parameters.
– No assignments within macro
Parameterized Macros cont’d
• Want to wrap malloc() calls.
– Can we?
• YES!
#define NEW_T(t,sz)
((t*) malloc((sz)*sizeof(t))
Appropriate use of Macros
• Do
– Use parentheses
– Handle header file inclusion
– Capitalize macro names
• Don’t
– Use more than 2 parameters.
– Replace function calls with macros for
‘performance’
– Make the code harder to read
#define LOOP(s) while(1) {s};
Conditional Inclusion
• #if
• #ifdef
• #ifndef
• #elif
• #else
• #endif
Conditional Compilation
• Debugging
#define DEBUG_MODE
#ifdef DEBUG_MODE
/* … Debug code … */
#endif
• OS/Platform
#ifdef _WIN32
#include <windows.h>
#elif
/* …… */
File Header
• File header
– #ifndef FOO_H
#define FOO_H
/* … the whole file … *
#endif
• Why???
– Global variables
– Cycles of includes
– Inefficient
The Printf
Yongjoon Lee
April 22, 2005
Basics
• printf(“format string”,vars);
• Format string?
– “This year is %dn”
– “Your score is %dn”
• Conversion by %
– %d : int
– %f : float, double
– %c : char
– %s : char *, string
– %e : float, double in scientific form
Examples %d conversion
• Padding ( width )
%12d
%012d
• Precision
%12.4d
%12.6d
• Left/right justification
%12d
%-12d
• Similar in %f, %e, … conversions
Example %c, %s conversion
• %c has only precision 1
• Precision in %s means string length
starting from the beginning
• Padding and justification are same

More Related Content

Similar to lec12.ppt

C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
EPORI
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
yp02
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
Infotech27
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
TeacherOnat
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
Michael Redlich
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
Michael Redlich
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
Pôle Systematic Paris-Region
 
Reproducible research
Reproducible researchReproducible research
Reproducible research
C. Tobin Magle
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
Henry Schreiner
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineni
abhishekl404
 
CPlusPus
CPlusPusCPlusPus
CPlusPus
rasen58
 
2015 bioinformatics bio_python
2015 bioinformatics bio_python2015 bioinformatics bio_python
2015 bioinformatics bio_python
Prof. Wim Van Criekinge
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
midnite_runr
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
Qiangning Hong
 
Effective CMake
Effective CMakeEffective CMake
Effective CMake
Daniel Pfeifer
 
201705 metaprogramming in julia
201705 metaprogramming in julia201705 metaprogramming in julia
201705 metaprogramming in julia
岳華 杜
 
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
 
R sharing 101
R sharing 101R sharing 101
R sharing 101
Omnia Safaan
 

Similar to lec12.ppt (20)

C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
 
Reproducible research
Reproducible researchReproducible research
Reproducible research
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineni
 
CPlusPus
CPlusPusCPlusPus
CPlusPus
 
2015 bioinformatics bio_python
2015 bioinformatics bio_python2015 bioinformatics bio_python
2015 bioinformatics bio_python
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
Effective CMake
Effective CMakeEffective CMake
Effective CMake
 
201705 metaprogramming in julia
201705 metaprogramming in julia201705 metaprogramming in julia
201705 metaprogramming in julia
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
R sharing 101
R sharing 101R sharing 101
R sharing 101
 

Recently uploaded

Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 

Recently uploaded (20)

Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 

lec12.ppt

  • 1. The C Preprocessor Yongjoon Lee April 22, 2005
  • 2. What is it? • Things with # – #include <stdio.h> • Processes the C source files BEFORE handing it to compiler. – `Pre`-process – gcc –E – cpp
  • 3. Why care? • File Inclusion • Basic Macros • Parameterized Macros • Conditional Compilation • Style and (mis)use of Macros
  • 4. File Inclusion • Recall : #include <filename> – #include <foo.h> • System directories – #include “foo.h” • Current directories – gcc -I
  • 5. Macros • We can #define constants #define HALF_PI 1.5708 #define BAD_PI HALF_PI+HALF_PI • Solution? ((((((((((The parentheses!)))))))))) #define PI ((HALF_PI)+(HALF_PI))
  • 6. Parameterized Macros • Macros with param : #define BAAAD_DOUBLE(x) (x*2) #define BAD_DOUBLE(x) ((x)+(x)) #define OKAY_DOUBLE(x) ((x)*2) • WHAT TO TAKE HOME: – Don’t use 2+ parameters. – No assignments within macro
  • 7. Parameterized Macros cont’d • Want to wrap malloc() calls. – Can we? • YES! #define NEW_T(t,sz) ((t*) malloc((sz)*sizeof(t))
  • 8. Appropriate use of Macros • Do – Use parentheses – Handle header file inclusion – Capitalize macro names • Don’t – Use more than 2 parameters. – Replace function calls with macros for ‘performance’ – Make the code harder to read #define LOOP(s) while(1) {s};
  • 9. Conditional Inclusion • #if • #ifdef • #ifndef • #elif • #else • #endif
  • 10. Conditional Compilation • Debugging #define DEBUG_MODE #ifdef DEBUG_MODE /* … Debug code … */ #endif • OS/Platform #ifdef _WIN32 #include <windows.h> #elif /* …… */
  • 11. File Header • File header – #ifndef FOO_H #define FOO_H /* … the whole file … * #endif • Why??? – Global variables – Cycles of includes – Inefficient
  • 13. Basics • printf(“format string”,vars); • Format string? – “This year is %dn” – “Your score is %dn” • Conversion by % – %d : int – %f : float, double – %c : char – %s : char *, string – %e : float, double in scientific form
  • 14. Examples %d conversion • Padding ( width ) %12d %012d • Precision %12.4d %12.6d • Left/right justification %12d %-12d • Similar in %f, %e, … conversions
  • 15. Example %c, %s conversion • %c has only precision 1 • Precision in %s means string length starting from the beginning • Padding and justification are same