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

DOCX
Maharishi University of Management (MSc Computer Science test questions)
PDF
Pointers in C language
PPTX
This pointer
DOCX
C interview question answer 2
PPTX
Pointer in C
PDF
C aptitude scribd
DOC
Captitude 2doc-100627004318-phpapp01
DOC
C aptitude.2doc
Maharishi University of Management (MSc Computer Science test questions)
Pointers in C language
This pointer
C interview question answer 2
Pointer in C
C aptitude scribd
Captitude 2doc-100627004318-phpapp01
C aptitude.2doc

What's hot (20)

PPTX
COM1407: Working with Pointers
PDF
PPT
Pointer in C
PPT
Pointers in C
PPT
Pointers+(2)
PPTX
Function Pointer
PPTX
Pointer in c
PPTX
C programming - Pointers
PPT
Ch 7-pointers
PPTX
Pointers
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
Data structure week 3
PPT
Ch5 array nota
PPT
C pointers
PPT
pointers
PDF
C language sample test
PPT
16 subroutine
PPT
detailed information about Pointers in c language
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
COM1407: Working with Pointers
Pointer in C
Pointers in C
Pointers+(2)
Function Pointer
Pointer in c
C programming - Pointers
Ch 7-pointers
Pointers
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Data structure week 3
Ch5 array nota
C pointers
pointers
C language sample test
16 subroutine
detailed information about Pointers in c language
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ad

Similar to CSE240 Macros and Preprocessing (20)

PPTX
Unit-IV.pptx
PDF
Preprocessor
PDF
6 preprocessor macro header
PDF
Module 05 Preprocessor and Macros in C
PPTX
1614745770521_vivek macros.pptx
PDF
05 -working_with_the_preproce
PDF
3 1. preprocessor, math, stdlib
PPT
c-programming
PDF
qb unit2 solve eem201.pdf
PPTX
c programming session 1.pptx
PDF
C intro
PPTX
Overview of C Mrs Sowmya Jyothi
PDF
ANSI C Macros
PDF
2 EPT 162 Lecture 2
PPTX
Lecture 01 Programming C for Beginners 001
PPT
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
PPTX
chapter-6 slide.pptx
PPTX
Preprocessor
PPTX
Programming Fundamentals lecture 5
Unit-IV.pptx
Preprocessor
6 preprocessor macro header
Module 05 Preprocessor and Macros in C
1614745770521_vivek macros.pptx
05 -working_with_the_preproce
3 1. preprocessor, math, stdlib
c-programming
qb unit2 solve eem201.pdf
c programming session 1.pptx
C intro
Overview of C Mrs Sowmya Jyothi
ANSI C Macros
2 EPT 162 Lecture 2
Lecture 01 Programming C for Beginners 001
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
chapter-6 slide.pptx
Preprocessor
Programming Fundamentals lecture 5
Ad

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;