SlideShare a Scribd company logo
1 of 18
Sri Balaji College of Engineering & Technology
MACRO in C
SUPERVISOR:-
MR. SANJAY SHARMA
Assistant Professor
SBCET , JAIPUR
SUBMITTED BY:-
NAME:- VIVEK SINGH RAJAWAT
ROLL NO. 19ESBCS020
BRANCH :- CSE
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Sri Balaji College of Engineering & Technology
Rajasthan Technical University , Kota
January 2021
A PRESENTATION ON MACRO
MACRO in C
CONTENTS
• MACROS:INTRO
• Using simple macros
• Parameterized macros
• Macros vs. functions
• Macros vs. Functions: Argument Evaluation
• Properties of macros
• Typedefs
• Conditional Compilation: #ifdef
• Solution to multiple inclusion problem
• Predefined Macros
Macros:INTRO
4
• A macro is a symbol that is recognized by the
preprocessor and replaced by the macro body
– Structure of simple macros:
#defineidentifierreplacement_list
– Examples:
#defin
e
BUFFER
SZ
102
4
#defin
e
WORDLE
N
64
Using simple macros
5
• We just use the macro name in place of the
value, e.g.:
#define BUFLEN
1024
#define Pi 3.1416
…
char
buffer[BUFLEN];
…
area = Pi * r * r;
NOT
: #define BUFLEN =
1024 #define Pi
3.1416;
Example 1
6
Parameterized macros
7
• Macros can have parameters
– these resemble functions in some ways:
• macro definition ~ formal parameters
• macro use ~ actual arguments
– Form:
#define macroName(arg1, …, argn)
replacement_list
– Example:
#define
deref(pt
r) #define
MAX(x,y
)
*ptr
x > y ? x
: y
no space
here!
(else preprocessor
will assume we’re
defining a simple
macro
Example
8
Macros vs. functions
• Macros may be (slightly) faster
– don’t incur the overhead of function call/return
– however, the resulting code size is usually larger
• this can lead to loss of speed
• Macros are “generic”
– parameters don’t have any associated type
– arguments are not type-checked
• Macros may evaluate their arguments more than once
•– a function argument is only evaluated once per call
9
•
•
Macros vs. Functions: Argument Evaluation
10
• Macros and functions may behave differently if an argument is referenced multiple times:
– a function argument is evaluated once, before the call
– a macro argument is evaluated each time it is
encountered in the macro body.
Example:
•
int dbl(x) { return x + x;}
…
u = 10; v = dbl(u++);
printf(“u = %d, v = %d”,
u, v);
prints: u = 11, v =
20
u = 10; v = Dbl(u++);
printf(“u = %d, v = %d”, u, v);
prints: u = 12, v =
21
Dbl(u++)
expands
to:
#define Dbl(x) x + x
…u++ + u++
Properties of macros
11
• Macros may be nested
– in definitions,
e.g.:
#define Pi 3.1416
2*Pi
#define Twice_Pi
– in uses, e.g.:
#define double(x) x+x
#define Pi 3.1416
…
if ( x > double(Pi)
) …
• Nested macros are expanded
recursively
typedefs
12
•
•
Allow us to define aliases for
types Syntax:
typedef old_type_name new_type_name;
• new_type_name becomes an alias for
old_type_name
Example:
•
– typedef int BasePay;
– typedef struct node {
int value;
struct node *next;
} node;
Conditional Compilation: #ifdef
13
#ifdef identifier
line 1
…
linen
#endif
•
• macros can be defined by the compiler:
– gcc –D macroName
– gcc –D macroName=definition
macros can be defined without giving them a
specific value, e.g.:
– #define macroName
line1 … linen will be included
if identifier has been defined
as a macro; otherwise
nothing will happen.
Conditional Compilation: #ifndef
14
#ifndefidentifier
line1
…
linen
#endi
f
line1 … linen will be
included if identifier
is NOT defined as a
macro; otherwise
nothing will happen.
Solution to multiple inclusion problem
15
The header file is written as
follows:
#ifndeffile_specific_flag
#define file_specific_flag
…contents of file…
#endif
• file_specific_flag usually constructed from the name of the header
file:
E.g.:file = foo.h  flag = _FOO_H_
– try to avoid macro names starting with
‘_’
indicates whether
or not this file has
been included
already
Another use of #ifdefs
16
• They can be useful for controlling debugging output
– Example 1: guard debugging code with #ifdefs:
#ifdef DEBUG
…debug message…
#endif
– Example 2: use the debug macro to control
what debugging code appears in the
program:
#ifdef DEBUG
#define
DMSG(msg)
printf(msg) // debugging
output
#else
#define
DMSG(msg)
{} // empty statement
#endif
straightforward, but
needs discipline to use
consistently
Generalizing #ifdef
17
#if constant-expression
line1
…
linen
#endif
 line1 … linen included if constant-expression evaluates to a non-zero value
Common
uses:
• #if 1
or
• #if 0
current line number of the source
file name of the current source
file
time of translation
1 if the compiler conforms to
ANSI C
LINE
FILE
TIME
STDC
printf("working on %sn", FILE );
Predefined Macros

More Related Content

Similar to 1614745770521_vivek macros.pptx

Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_iNico Ludwig
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11patcha535
 
Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor skbansal222
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproceHector Garzo
 
Practical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlibPractical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlibDatabricks
 
C and CPP Interview Questions
C and CPP Interview QuestionsC and CPP Interview Questions
C and CPP Interview QuestionsSagar Joshi
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain艾鍗科技
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfacsmadurai
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsVisual Engineering
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 

Similar to 1614745770521_vivek macros.pptx (20)

Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i
 
Chapter 13.1.11
Chapter 13.1.11Chapter 13.1.11
Chapter 13.1.11
 
Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
 
Practical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlibPractical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlib
 
Modular programming
Modular programmingModular programming
Modular programming
 
C and CPP Interview Questions
C and CPP Interview QuestionsC and CPP Interview Questions
C and CPP Interview Questions
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
Preprocesser in c
Preprocesser in cPreprocesser in c
Preprocesser in c
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design Patterns
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 

Recently uploaded

FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 

Recently uploaded (20)

Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 

1614745770521_vivek macros.pptx

  • 1. Sri Balaji College of Engineering & Technology MACRO in C SUPERVISOR:- MR. SANJAY SHARMA Assistant Professor SBCET , JAIPUR SUBMITTED BY:- NAME:- VIVEK SINGH RAJAWAT ROLL NO. 19ESBCS020 BRANCH :- CSE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Sri Balaji College of Engineering & Technology Rajasthan Technical University , Kota January 2021
  • 2. A PRESENTATION ON MACRO MACRO in C
  • 3. CONTENTS • MACROS:INTRO • Using simple macros • Parameterized macros • Macros vs. functions • Macros vs. Functions: Argument Evaluation • Properties of macros • Typedefs • Conditional Compilation: #ifdef • Solution to multiple inclusion problem • Predefined Macros
  • 4. Macros:INTRO 4 • A macro is a symbol that is recognized by the preprocessor and replaced by the macro body – Structure of simple macros: #defineidentifierreplacement_list – Examples: #defin e BUFFER SZ 102 4 #defin e WORDLE N 64
  • 5. Using simple macros 5 • We just use the macro name in place of the value, e.g.: #define BUFLEN 1024 #define Pi 3.1416 … char buffer[BUFLEN]; … area = Pi * r * r; NOT : #define BUFLEN = 1024 #define Pi 3.1416;
  • 7. Parameterized macros 7 • Macros can have parameters – these resemble functions in some ways: • macro definition ~ formal parameters • macro use ~ actual arguments – Form: #define macroName(arg1, …, argn) replacement_list – Example: #define deref(pt r) #define MAX(x,y ) *ptr x > y ? x : y no space here! (else preprocessor will assume we’re defining a simple macro
  • 9. Macros vs. functions • Macros may be (slightly) faster – don’t incur the overhead of function call/return – however, the resulting code size is usually larger • this can lead to loss of speed • Macros are “generic” – parameters don’t have any associated type – arguments are not type-checked • Macros may evaluate their arguments more than once •– a function argument is only evaluated once per call 9 • •
  • 10. Macros vs. Functions: Argument Evaluation 10 • Macros and functions may behave differently if an argument is referenced multiple times: – a function argument is evaluated once, before the call – a macro argument is evaluated each time it is encountered in the macro body. Example: • int dbl(x) { return x + x;} … u = 10; v = dbl(u++); printf(“u = %d, v = %d”, u, v); prints: u = 11, v = 20 u = 10; v = Dbl(u++); printf(“u = %d, v = %d”, u, v); prints: u = 12, v = 21 Dbl(u++) expands to: #define Dbl(x) x + x …u++ + u++
  • 11. Properties of macros 11 • Macros may be nested – in definitions, e.g.: #define Pi 3.1416 2*Pi #define Twice_Pi – in uses, e.g.: #define double(x) x+x #define Pi 3.1416 … if ( x > double(Pi) ) … • Nested macros are expanded recursively
  • 12. typedefs 12 • • Allow us to define aliases for types Syntax: typedef old_type_name new_type_name; • new_type_name becomes an alias for old_type_name Example: • – typedef int BasePay; – typedef struct node { int value; struct node *next; } node;
  • 13. Conditional Compilation: #ifdef 13 #ifdef identifier line 1 … linen #endif • • macros can be defined by the compiler: – gcc –D macroName – gcc –D macroName=definition macros can be defined without giving them a specific value, e.g.: – #define macroName line1 … linen will be included if identifier has been defined as a macro; otherwise nothing will happen.
  • 14. Conditional Compilation: #ifndef 14 #ifndefidentifier line1 … linen #endi f line1 … linen will be included if identifier is NOT defined as a macro; otherwise nothing will happen.
  • 15. Solution to multiple inclusion problem 15 The header file is written as follows: #ifndeffile_specific_flag #define file_specific_flag …contents of file… #endif • file_specific_flag usually constructed from the name of the header file: E.g.:file = foo.h  flag = _FOO_H_ – try to avoid macro names starting with ‘_’ indicates whether or not this file has been included already
  • 16. Another use of #ifdefs 16 • They can be useful for controlling debugging output – Example 1: guard debugging code with #ifdefs: #ifdef DEBUG …debug message… #endif – Example 2: use the debug macro to control what debugging code appears in the program: #ifdef DEBUG #define DMSG(msg) printf(msg) // debugging output #else #define DMSG(msg) {} // empty statement #endif straightforward, but needs discipline to use consistently
  • 17. Generalizing #ifdef 17 #if constant-expression line1 … linen #endif  line1 … linen included if constant-expression evaluates to a non-zero value Common uses: • #if 1 or • #if 0
  • 18. current line number of the source file name of the current source file time of translation 1 if the compiler conforms to ANSI C LINE FILE TIME STDC printf("working on %sn", FILE ); Predefined Macros