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;

CSE240 Macros and Preprocessing

  • 1.
  • 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 aMacro?  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: Beforeand 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> #defineINCREMENTER(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> #definePI 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> #definePI 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> #definePI 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> #definePI 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;