SlideShare a Scribd company logo
See through C
Module 5
Macros and preprocessors
Tushar B Kute
http://tusharkute.com
The C preprocessor and its role
2
cpp
(C preprocessor)
cc1
(C compiler)
source
program
compiled
code
C compiler (e.g., gcc)
expanded
code
• expand some kinds of characters
• discard whitespace and comments
– each comment is replaced with a single space
• process directives:
– file inclusion (#include)
– macro expansion (#define)
– conditional compilation (#if, #ifdef, …)
#include
• Specifies that the preprocessor should read in the contents of the specified file
– usually used to read in type definitions, prototypes, etc.
– proceeds recursively
• #includes in the included file are read in as well
• Two forms:
– #include <filename>
• searches for filename from a predefined list of directories
• the list can be extended via “gcc –I dir”
– #include “filename”
• looks for filename specified as a relative or absolute path
3
#include : Example
4
a predefined include file that:
• comes with the system
• gives type declarations,
prototypes for library routines
(printf)
where does it come from?
– man 3 printf :
#include: cont’d
• We can also define our own header files:
– a header file has file-extension ‘.h’
– these header files typically contain “public” information
• type declarations
• macros and other definitions
• function prototypes
– often, the public information associated with a code file
foo.c will be placed in a header file foo.h
– these header files are included by files that need that
public information
#include “myheaderfile.h”
5
Macros
• A macro is a symbol that is recognized by the preprocessor and
replaced by the macro body
– Structure of simple macros:
#define identifier replacement_list
– Examples:
#define BUFFERSZ 1024
#define WORDLEN 64
6
Using simple macros
• 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;
7
NOT:
#define BUFLEN = 1024
#define Pi 3.1416;

Example 1
8
Example 2
9
we can “macroize”
symbols selectively
Parameterized macros
• 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(ptr) *ptr
#define MAX(x,y) x > y ? x : y
10
no space here!
(else preprocessor will
assume we’re defining
a simple macro
Example
11
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
12
Macros vs. Functions: Argument Evaluation
• 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:
13
int dbl(x) { return x + x;}
…
u = 10; v = dbl(u++);
printf(“u = %d, v = %d”, u, v);
prints: u = 11, v = 20
#define Dbl(x) x + x
…
u = 10; v = Dbl(u++);
printf(“u = %d, v = %d”, u, v);
prints: u = 12, v = 21
Dbl(u++)
expands to:
u++ + u++
Properties of macros
• Macros may be nested
– in definitions, e.g.:
#define Pi 3.1416
#define Twice_Pi 2*Pi
– in uses, e.g.:
#define double(x) x+x
#define Pi 3.1416
…
if ( x > double(Pi) ) …
• Nested macros are expanded recursively
14
Header Files
• Have a file extension “.h”
• Contain shared definitions
– typedefs
– macros
– function prototypes
• referenced via “#include” directives
15
Header files: example
16
typedefs
• 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;
17
Example
18
defines “wcnode” as an
alias for “struct wc”
we can use “wcnode” in
place of“struct wc”
but not here, since
“wcnode” has not yet
been defined
What if a file is #included multiple times?
19
foo.h
bar1.h bar2.h
bar.c
Conditional Compilation: #ifdef
#ifdef identifier
line1
…
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
20
line1 … linen will be included if
identifier has been defined as a
macro; otherwise nothing will
happen.
Conditional Compilation: #ifndef
#ifndef identifier
line1
…
linen
#endif
21
line1 … linen will be
included if identifier
is NOT defined as a
macro; otherwise
nothing will happen.
Solution to multiple inclusion problem
The header file is written as follows:
#ifndef file_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 ‘_’
22
indicates whether or
not this file has been
included already
Another use of #ifdefs
• 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
23
straightforward, but needs
discipline to use consistently
Generalizing #ifdef
#if constant-expression
line1
…
linen
#endif
⇒ line1 … linen included if constant-expression evaluates to a non-zero value
24
Common uses:
• #if 1
or
• #if 0
__LINE__ current line number of the source file
__FILE__ name of the current source file
__TIME__ time of translation
__STDC__ 1 if the compiler conforms to ANSI C
printf("working on %sn", __FILE__);
Predefined Macros
Adapted originally from:
CSc 352
An Introduction to the C Preprocessor
Saumya Debray
Dept. of Computer Science
The University of Arizona, Tucson
debray@cs.arizona.edu
Thank you
This presentation is created using LibreOffice Impress 3.6.2.2

More Related Content

What's hot

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
lalithambiga kamaraj
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming9
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
tanmaymodi4
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
ASHABOOPATHY
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
C pointer
C pointerC pointer

What's hot (20)

Strings
StringsStrings
Strings
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Function in C
Function in CFunction in C
Function in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
C pointer
C pointerC pointer
C pointer
 

Viewers also liked

Macro
MacroMacro
Macro
Google
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit IIManoj Patil
 
The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
iuui
 
Embedded C workshop
Embedded C workshopEmbedded C workshop
Embedded C workshop
Mostafa El-koumy
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
Mohamed Abdallah
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
Aman Sharma
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
manishpatel_79
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
Embedded System Basics
Embedded System BasicsEmbedded System Basics
Embedded System Basics
Dr M Muruganandam Masilamani
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051Quản Minh Tú
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
SlideShare
 

Viewers also liked (14)

pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Macro
MacroMacro
Macro
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
Embedded C workshop
Embedded C workshopEmbedded C workshop
Embedded C workshop
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 
Embedded System Basics
Embedded System BasicsEmbedded System Basics
Embedded System Basics
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 

Similar to Module 05 Preprocessor and Macros in C

1614745770521_vivek macros.pptx
1614745770521_vivek macros.pptx1614745770521_vivek macros.pptx
1614745770521_vivek macros.pptx
VivekSaini87
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
AlAmos4
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
hasan Mohammad
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain
艾鍗科技
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
M-TEC Computer Education
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
Thesis Scientist Private Limited
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
Atul Setu
 
cppProgramStructure.ppt
cppProgramStructure.pptcppProgramStructure.ppt
cppProgramStructure.ppt
DaveCalapis4
 
presentation_preprocessors_1454990964_183807.ppt
presentation_preprocessors_1454990964_183807.pptpresentation_preprocessors_1454990964_183807.ppt
presentation_preprocessors_1454990964_183807.ppt
ChetanChavan124116
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
Võ Hòa
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
rumanatasnim415
 
C++primer
C++primerC++primer
C++primer
leonlongli
 

Similar to Module 05 Preprocessor and Macros in C (20)

1614745770521_vivek macros.pptx
1614745770521_vivek macros.pptx1614745770521_vivek macros.pptx
1614745770521_vivek macros.pptx
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
cppProgramStructure.ppt
cppProgramStructure.pptcppProgramStructure.ppt
cppProgramStructure.ppt
 
presentation_preprocessors_1454990964_183807.ppt
presentation_preprocessors_1454990964_183807.pptpresentation_preprocessors_1454990964_183807.ppt
presentation_preprocessors_1454990964_183807.ppt
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
C++primer
C++primerC++primer
C++primer
 

More from Tushar B Kute

Apache Pig: A big data processor
Apache Pig: A big data processorApache Pig: A big data processor
Apache Pig: A big data processor
Tushar B Kute
 
01 Introduction to Android
01 Introduction to Android01 Introduction to Android
01 Introduction to Android
Tushar B Kute
 
Ubuntu OS and it's Flavours
Ubuntu OS and it's FlavoursUbuntu OS and it's Flavours
Ubuntu OS and it's Flavours
Tushar B Kute
 
Install Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteInstall Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. Kute
Tushar B Kute
 
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteInstall Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Tushar B Kute
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
Tushar B Kute
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
Tushar B Kute
 
Implementation of FIFO in Linux
Implementation of FIFO in LinuxImplementation of FIFO in Linux
Implementation of FIFO in Linux
Tushar B Kute
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
Tushar B Kute
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
Tushar B Kute
 
Part 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxPart 04 Creating a System Call in Linux
Part 04 Creating a System Call in Linux
Tushar B Kute
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
Tushar B Kute
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
Tushar B Kute
 
Open source applications softwares
Open source applications softwaresOpen source applications softwares
Open source applications softwares
Tushar B Kute
 
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Tushar B Kute
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Tushar B Kute
 
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTechnical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Tushar B Kute
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 

More from Tushar B Kute (20)

Apache Pig: A big data processor
Apache Pig: A big data processorApache Pig: A big data processor
Apache Pig: A big data processor
 
01 Introduction to Android
01 Introduction to Android01 Introduction to Android
01 Introduction to Android
 
Ubuntu OS and it's Flavours
Ubuntu OS and it's FlavoursUbuntu OS and it's Flavours
Ubuntu OS and it's Flavours
 
Install Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteInstall Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. Kute
 
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteInstall Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. Kute
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Implementation of FIFO in Linux
Implementation of FIFO in LinuxImplementation of FIFO in Linux
Implementation of FIFO in Linux
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Part 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxPart 04 Creating a System Call in Linux
Part 04 Creating a System Call in Linux
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
 
Open source applications softwares
Open source applications softwaresOpen source applications softwares
Open source applications softwares
 
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTechnical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrc
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 

Recently uploaded

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 

Recently uploaded (20)

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 

Module 05 Preprocessor and Macros in C

  • 1. See through C Module 5 Macros and preprocessors Tushar B Kute http://tusharkute.com
  • 2. The C preprocessor and its role 2 cpp (C preprocessor) cc1 (C compiler) source program compiled code C compiler (e.g., gcc) expanded code • expand some kinds of characters • discard whitespace and comments – each comment is replaced with a single space • process directives: – file inclusion (#include) – macro expansion (#define) – conditional compilation (#if, #ifdef, …)
  • 3. #include • Specifies that the preprocessor should read in the contents of the specified file – usually used to read in type definitions, prototypes, etc. – proceeds recursively • #includes in the included file are read in as well • Two forms: – #include <filename> • searches for filename from a predefined list of directories • the list can be extended via “gcc –I dir” – #include “filename” • looks for filename specified as a relative or absolute path 3
  • 4. #include : Example 4 a predefined include file that: • comes with the system • gives type declarations, prototypes for library routines (printf) where does it come from? – man 3 printf :
  • 5. #include: cont’d • We can also define our own header files: – a header file has file-extension ‘.h’ – these header files typically contain “public” information • type declarations • macros and other definitions • function prototypes – often, the public information associated with a code file foo.c will be placed in a header file foo.h – these header files are included by files that need that public information #include “myheaderfile.h” 5
  • 6. Macros • A macro is a symbol that is recognized by the preprocessor and replaced by the macro body – Structure of simple macros: #define identifier replacement_list – Examples: #define BUFFERSZ 1024 #define WORDLEN 64 6
  • 7. Using simple macros • 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; 7 NOT: #define BUFLEN = 1024 #define Pi 3.1416; 
  • 9. Example 2 9 we can “macroize” symbols selectively
  • 10. Parameterized macros • 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(ptr) *ptr #define MAX(x,y) x > y ? x : y 10 no space here! (else preprocessor will assume we’re defining a simple macro
  • 12. 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 12
  • 13. Macros vs. Functions: Argument Evaluation • 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: 13 int dbl(x) { return x + x;} … u = 10; v = dbl(u++); printf(“u = %d, v = %d”, u, v); prints: u = 11, v = 20 #define Dbl(x) x + x … u = 10; v = Dbl(u++); printf(“u = %d, v = %d”, u, v); prints: u = 12, v = 21 Dbl(u++) expands to: u++ + u++
  • 14. Properties of macros • Macros may be nested – in definitions, e.g.: #define Pi 3.1416 #define Twice_Pi 2*Pi – in uses, e.g.: #define double(x) x+x #define Pi 3.1416 … if ( x > double(Pi) ) … • Nested macros are expanded recursively 14
  • 15. Header Files • Have a file extension “.h” • Contain shared definitions – typedefs – macros – function prototypes • referenced via “#include” directives 15
  • 17. typedefs • 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; 17
  • 18. Example 18 defines “wcnode” as an alias for “struct wc” we can use “wcnode” in place of“struct wc” but not here, since “wcnode” has not yet been defined
  • 19. What if a file is #included multiple times? 19 foo.h bar1.h bar2.h bar.c
  • 20. Conditional Compilation: #ifdef #ifdef identifier line1 … 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 20 line1 … linen will be included if identifier has been defined as a macro; otherwise nothing will happen.
  • 21. Conditional Compilation: #ifndef #ifndef identifier line1 … linen #endif 21 line1 … linen will be included if identifier is NOT defined as a macro; otherwise nothing will happen.
  • 22. Solution to multiple inclusion problem The header file is written as follows: #ifndef file_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 ‘_’ 22 indicates whether or not this file has been included already
  • 23. Another use of #ifdefs • 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 23 straightforward, but needs discipline to use consistently
  • 24. Generalizing #ifdef #if constant-expression line1 … linen #endif ⇒ line1 … linen included if constant-expression evaluates to a non-zero value 24 Common uses: • #if 1 or • #if 0
  • 25. __LINE__ current line number of the source file __FILE__ name of the current source file __TIME__ time of translation __STDC__ 1 if the compiler conforms to ANSI C printf("working on %sn", __FILE__); Predefined Macros
  • 26. Adapted originally from: CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu Thank you This presentation is created using LibreOffice Impress 3.6.2.2