To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."
Quiz
What's wrong with the following macro definition?
#define ONE 1;
What is the final value assigned to result after the assignment statement is executed?
#define ONE 1
#define NINE 9
#define EXPRESS ONE + NINE
result = EXPRESS * NINE;
What message will be printed out from the following code segment?
#define MACRO_NAME 0
#if MACRO_NAME
printf("Under #if.\n");
#else
printf("Under #else.\n");
#endif
What message will be printed out from the following code segment?
#define MACRO_NAME 0
#ifdef MACRO_NAME
printf("Under #ifdef.\n");
#endif
#ifndef MACRO_NAME
printf("Under #ifndef.\n");
#endif
Exercises
In Hour 18, "More Data Types and Functions," you learned how to define enum data. Rewrite the program in Listing 18.1 with the #define directive.
Define a macro name that can multiply two arguments. Write a program to calculate the multiplication of 2 and 3 with the help of the macro. Print out the result of the program.
Rewrite the program in Listing 23.2 with the #if, #elif, and #else directives.
Rewrite the program in Listing 23.3 with nested #if directives.
***
- 59 views