SlideShare a Scribd company logo
CSE240
Macros and Preprocessing
What is preprocessing?
 It is the state of the program that occurs BEFORE any
code is compiled
 At this point, no values are initialized and nothing is
evaluated
 Code swapping and replacing can occur here, but
no computations
What is a Macro?
 A macro, as defined in C and C++, substitutes a definition (a.k.a.
code) for a name in a program
 Basically, this says that a Macro acts like a find an replace function given a
name and it replaces it with some text (called a definition)
 Macros allow arguments which create “in-line” functions or
procedures
 Since macros only substitute code, nothing is actually processed or
evaluated
 Ex: #define PI 3.14
 PI is not a variable with a value assigned to be 3.14
 Whenever the compiler sees PI it replaces the text with 3.14
Macro Preprocessing: Before and After
Before Preprocessing
#include <stdio.h>
#define GREETING "Hello, my name is Sam.n"
#define QUESTION "What is the product of 4 and 7?n"
#define COMPUTATION 4*7
#define FAREWELL "The answer is %d, Goodbye!n"
int main()
{
int product = 0;
printf(GREETING);
printf(QUESTION);
product = COMPUTATION;
printf(FAREWELL, product);
return 0;
}
After Preprocessing but Before Compilation
#include <stdio.h>
#define GREETING "Hello, my name is Sam.n"
#define QUESTION "What is the product of 4 and 7?n"
#define COMPUTATION 4*7
#define FAREWELL "The answer is %d, Goodbye!n"
int main()
{
int product = 0;
printf("Hello, my name is Sam.n");
printf("What is the product of 4 and 7?n");
product = 4*7;
printf("The answer is %d, Goodbye!n", product);
return 0;
}
Macro In-Line Functions
 Uses notation similar to a function, but does not do any
computation or evaluation; it only finds and replaces text
 Example: #define RECTANGLE_AREA(length, width)
length*width
 If RECTANGLE_AREA(3,10) were used in a function, the text
“length” with would be replaced with “3”, which would be
followed by “*”, and then the text “width” would be replaced
by “10”
 int x = RECTANGLE_AREA(3,10); becomes int x = 3*10; after
preprocessing
Macro Side Effects
 Since macros substitute code, using the pre-increment ++x and
post increment x++ has unintended results. The same is true for
the pre and post decrements.
Ex: #define function(x, y) ( x < y) ? x : y
int x = 1, int y = 5;
If you call function(++x, y), it is replaced with (++x < y ) ? ++x : y.
When compiled, it does (++x < y) ? ++x : y.
x is now 2 x is now 3
At the end, x will be 3, y will be 5, and the result of the computation is 3 because it
calculates ++x twice and then returns it.
Question 1
#include <stdio.h>
#define INCREMENTER(a,b) (a > b) ? b : a
int main()
{
int a = 2;
int b = 3;
int result;
result = INCREMENTER(a++, b);
printf("a is %d, b is %d, and result is %dn", a, b,
result);
result = INCREMENTER(++a, b);
printf("a is %d, b is %d, and result is %dn", a, b,
result);
return 0;
}
 What is the output of the program?
A.) a is 4, b is 3, and result is 3
a is 5, b is 3, and result is 3
B.) a is 3, b is 3, and result is 3
a is 4, b is 3, and result is 3
C.) a is 2, b is 3, and result is 2
a is 2, b is 3, and result is 2
D.) a is 4, b is 3, and result is 4
a is 5, b is 3, and result is 3
Question 2
#include <stdio.h>
#define PI 3.14159265
#define DENSITY(mass,volume) mass/volume // Q2
#define CIRCUMFERENCE(r) 2*r*PI
#define INITIALIZE_FLOAT(name, value) float name =
value
int main()
{
INITIALIZE_FLOAT(density, 0.0);
INITIALIZE_FLOAT(circumference, 0.0);
INITIALIZE_FLOAT(radius, 5.0); // Q3
density = DENSITY(10.0, 2.0); // Q4
circumference = CIRCUMFERENCE(radius); // Q5
return 0;
}
 How does the following macro work during
preprocessing?
#define DENSITY(mass,volume) mass/volume
A.) Two local variables are created then divided
B.) Two passed in parameters are divided and the
result is returned
C.) The words mass and volume are substituted by
corresponding values in the parameter and the divide
symbol is placed between them.
D.) The compiler evaluates both parameters and
performs a calculation
Question 3
#include <stdio.h>
#define PI 3.14159265
#define DENSITY(mass,volume) mass/volume // Q2
#define CIRCUMFERENCE(r) 2*r*PI
#define INITIALIZE_FLOAT(name, value) float name = value
int main()
{
INITIALIZE_FLOAT(density, 0.0);
INITIALIZE_FLOAT(circumference, 0.0);
INITIALIZE_FLOAT(radius, 5.0); // Q3
density = DENSITY(10.0, 2.0); // Q4
circumference = CIRCUMFERENCE(radius); // Q5
return 0;
}
 At line Q3 and after preprocessing but before
compilation, what would this line look like?
A.) INITIALIZE_FLOAT(radius, 5.0);
B.) float radius = 5.0;
C.) radius = 5.0;
D.) INITIALIZE_FLOAT(radius, 5.0) float name =
value;
Question 4
#include <stdio.h>
#define PI 3.14159265
#define DENSITY(mass,volume) mass/volume // Q2
#define CIRCUMFERENCE(r) 2*r*PI
#define INITIALIZE_FLOAT(name, value) float name = value
int main()
{
INITIALIZE_FLOAT(density, 0.0);
INITIALIZE_FLOAT(circumference, 0.0);
INITIALIZE_FLOAT(radius, 5.0); // Q3
density = DENSITY(10.0, 2.0); // Q4
circumference = CIRCUMFERENCE(radius); // Q5
return 0;
}
 At line Q4 and after preprocessing but
before compilation, what would this line
look like?
A.) density = 10.0/2.0;
B.) density = 5.0;
C.) density = DENSITY(10.0, 2.0) mass/volume;
D.) density = DENSITY(10.0, 2.0);
Question 5
#include <stdio.h>
#define PI 3.14159265
#define DENSITY(mass,volume) mass/volume // Q2
#define CIRCUMFERENCE(r) 2*r*PI
#define INITIALIZE_FLOAT(name, value) float name = value
int main()
{
INITIALIZE_FLOAT(density, 0.0);
INITIALIZE_FLOAT(circumference, 0.0);
INITIALIZE_FLOAT(radius, 5.0); // Q3
density = DENSITY(10.0, 2.0); // Q4
circumference = CIRCUMFERENCE(radius); // Q5
return 0;
}
 At line Q5 and after preprocessing but
before compilation, what would this line
look like?
A.) circumference = CIRCUMFERENCE(radius);
B.) circumference = CIRCUMFERENCE(5.0);
C.) circumference = 31.416;
D.) circumference = 2*radius*3.14159265;

More Related Content

What's hot

COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Pointers
PointersPointers
Pointer in C
Pointer in CPointer in C
Pointer in C
Sonya Akter Rupa
 
Pointers in C
Pointers in CPointers in C
Pointers in C
guestdc3f16
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
Rubal Bansal
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
Dr-Dipali Meher
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
Muslimee Subhany
 
C pointer basics
C pointer basicsC pointer basics
Pointers
PointersPointers
Pointers
Samsil Arefin
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
Hattori Sidek
 
C pointers
C pointersC pointers
C pointers
Aravind Mohan
 
pointers
pointerspointers
pointers
teach4uin
 
C language sample test
C language sample testC language sample test
C language sample test
Sompal Duhan
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
fyjordan9
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 

What's hot (20)

COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers
PointersPointers
Pointers
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers
PointersPointers
Pointers
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
C pointers
C pointersC pointers
C pointers
 
pointers
pointerspointers
pointers
 
C language sample test
C language sample testC language sample test
C language sample test
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 

Similar to CSE240 Macros and Preprocessing

Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
Tushar B Kute
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
C++
C++C++
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4
NIMMYRAJU
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Ch3
Ch3Ch3
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
REHAN IJAZ
 
Array Cont
Array ContArray Cont

Similar to CSE240 Macros and Preprocessing (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Bc0037
Bc0037Bc0037
Bc0037
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Ch3
Ch3Ch3
Ch3
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Array Cont
Array ContArray Cont
Array Cont
 

CSE240 Macros and Preprocessing

  • 2. What is preprocessing?  It is the state of the program that occurs BEFORE any code is compiled  At this point, no values are initialized and nothing is evaluated  Code swapping and replacing can occur here, but no computations
  • 3. What is a Macro?  A macro, as defined in C and C++, substitutes a definition (a.k.a. code) for a name in a program  Basically, this says that a Macro acts like a find an replace function given a name and it replaces it with some text (called a definition)  Macros allow arguments which create “in-line” functions or procedures  Since macros only substitute code, nothing is actually processed or evaluated  Ex: #define PI 3.14  PI is not a variable with a value assigned to be 3.14  Whenever the compiler sees PI it replaces the text with 3.14
  • 4. Macro Preprocessing: Before and After Before Preprocessing #include <stdio.h> #define GREETING "Hello, my name is Sam.n" #define QUESTION "What is the product of 4 and 7?n" #define COMPUTATION 4*7 #define FAREWELL "The answer is %d, Goodbye!n" int main() { int product = 0; printf(GREETING); printf(QUESTION); product = COMPUTATION; printf(FAREWELL, product); return 0; } After Preprocessing but Before Compilation #include <stdio.h> #define GREETING "Hello, my name is Sam.n" #define QUESTION "What is the product of 4 and 7?n" #define COMPUTATION 4*7 #define FAREWELL "The answer is %d, Goodbye!n" int main() { int product = 0; printf("Hello, my name is Sam.n"); printf("What is the product of 4 and 7?n"); product = 4*7; printf("The answer is %d, Goodbye!n", product); return 0; }
  • 5. Macro In-Line Functions  Uses notation similar to a function, but does not do any computation or evaluation; it only finds and replaces text  Example: #define RECTANGLE_AREA(length, width) length*width  If RECTANGLE_AREA(3,10) were used in a function, the text “length” with would be replaced with “3”, which would be followed by “*”, and then the text “width” would be replaced by “10”  int x = RECTANGLE_AREA(3,10); becomes int x = 3*10; after preprocessing
  • 6. Macro Side Effects  Since macros substitute code, using the pre-increment ++x and post increment x++ has unintended results. The same is true for the pre and post decrements. Ex: #define function(x, y) ( x < y) ? x : y int x = 1, int y = 5; If you call function(++x, y), it is replaced with (++x < y ) ? ++x : y. When compiled, it does (++x < y) ? ++x : y. x is now 2 x is now 3 At the end, x will be 3, y will be 5, and the result of the computation is 3 because it calculates ++x twice and then returns it.
  • 7. Question 1 #include <stdio.h> #define INCREMENTER(a,b) (a > b) ? b : a int main() { int a = 2; int b = 3; int result; result = INCREMENTER(a++, b); printf("a is %d, b is %d, and result is %dn", a, b, result); result = INCREMENTER(++a, b); printf("a is %d, b is %d, and result is %dn", a, b, result); return 0; }  What is the output of the program? A.) a is 4, b is 3, and result is 3 a is 5, b is 3, and result is 3 B.) a is 3, b is 3, and result is 3 a is 4, b is 3, and result is 3 C.) a is 2, b is 3, and result is 2 a is 2, b is 3, and result is 2 D.) a is 4, b is 3, and result is 4 a is 5, b is 3, and result is 3
  • 8. Question 2 #include <stdio.h> #define PI 3.14159265 #define DENSITY(mass,volume) mass/volume // Q2 #define CIRCUMFERENCE(r) 2*r*PI #define INITIALIZE_FLOAT(name, value) float name = value int main() { INITIALIZE_FLOAT(density, 0.0); INITIALIZE_FLOAT(circumference, 0.0); INITIALIZE_FLOAT(radius, 5.0); // Q3 density = DENSITY(10.0, 2.0); // Q4 circumference = CIRCUMFERENCE(radius); // Q5 return 0; }  How does the following macro work during preprocessing? #define DENSITY(mass,volume) mass/volume A.) Two local variables are created then divided B.) Two passed in parameters are divided and the result is returned C.) The words mass and volume are substituted by corresponding values in the parameter and the divide symbol is placed between them. D.) The compiler evaluates both parameters and performs a calculation
  • 9. Question 3 #include <stdio.h> #define PI 3.14159265 #define DENSITY(mass,volume) mass/volume // Q2 #define CIRCUMFERENCE(r) 2*r*PI #define INITIALIZE_FLOAT(name, value) float name = value int main() { INITIALIZE_FLOAT(density, 0.0); INITIALIZE_FLOAT(circumference, 0.0); INITIALIZE_FLOAT(radius, 5.0); // Q3 density = DENSITY(10.0, 2.0); // Q4 circumference = CIRCUMFERENCE(radius); // Q5 return 0; }  At line Q3 and after preprocessing but before compilation, what would this line look like? A.) INITIALIZE_FLOAT(radius, 5.0); B.) float radius = 5.0; C.) radius = 5.0; D.) INITIALIZE_FLOAT(radius, 5.0) float name = value;
  • 10. Question 4 #include <stdio.h> #define PI 3.14159265 #define DENSITY(mass,volume) mass/volume // Q2 #define CIRCUMFERENCE(r) 2*r*PI #define INITIALIZE_FLOAT(name, value) float name = value int main() { INITIALIZE_FLOAT(density, 0.0); INITIALIZE_FLOAT(circumference, 0.0); INITIALIZE_FLOAT(radius, 5.0); // Q3 density = DENSITY(10.0, 2.0); // Q4 circumference = CIRCUMFERENCE(radius); // Q5 return 0; }  At line Q4 and after preprocessing but before compilation, what would this line look like? A.) density = 10.0/2.0; B.) density = 5.0; C.) density = DENSITY(10.0, 2.0) mass/volume; D.) density = DENSITY(10.0, 2.0);
  • 11. Question 5 #include <stdio.h> #define PI 3.14159265 #define DENSITY(mass,volume) mass/volume // Q2 #define CIRCUMFERENCE(r) 2*r*PI #define INITIALIZE_FLOAT(name, value) float name = value int main() { INITIALIZE_FLOAT(density, 0.0); INITIALIZE_FLOAT(circumference, 0.0); INITIALIZE_FLOAT(radius, 5.0); // Q3 density = DENSITY(10.0, 2.0); // Q4 circumference = CIRCUMFERENCE(radius); // Q5 return 0; }  At line Q5 and after preprocessing but before compilation, what would this line look like? A.) circumference = CIRCUMFERENCE(radius); B.) circumference = CIRCUMFERENCE(5.0); C.) circumference = 31.416; D.) circumference = 2*radius*3.14159265;