Chapter 14 - Worked Exercises 1

Exercise 1 - Preprocessor & Compiler

  1. What is the preprocessor?

  2. How are preprocessor directives declared/defined?

  3. Is the following statement true? Macros can be defined to compute certain values at compile time.

  4. What is the effect of an #include statement?

Exercise 2 - Macros

  1. What is the syntax for declaring a macro constant? Show an example of declaring a macro constant, and using it in a program.

  2. List 2 issues that come with using macro constants.

  3. What is the preferred method of defining/declaring program-wide constants?

Exercise 3 - Preprocessor Output

Study each of the code snippets below, and write the preprocessor output of the following programs:

#define VALUE_X 10
#define VALUE_Y 20

int main() {
    int a = VALUE_X;
    int b = VALUE_Y;
    int c = a + b;
}
#ifndef TEST 

#define VALUE_X 10
#define VALUE_Y 20 

#else

#define VALUE_X 100
#define VALUE_Y 200

#endif


int main() {
    int a = VALUE_X;
    int b = VALUE_Y;
    int c = a + b;
}
#define TEST
#ifndef TEST 

#define VALUE_X 10
#define VALUE_Y 20 

#else

#define VALUE_X 100
#define VALUE_Y 200

#endif


int main() {
    int a = VALUE_X;
    int b = VALUE_Y;
    int c = a + b;
}

Last updated

Was this helpful?