Exercises : Answer the following Question

Submitted by tushar pramanick on Mon, 03/11/2013 - 00:14

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.

 

***

Related Items

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

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.

Question and Answer

    Q Why is random access to a disk file necessary?

More Examples of Disk File I/O

More Examples of Disk File I/O

The following sections show several more examples of disk file I/O, such as reading and writing binary data and redirecting the standard streams. Three more I/O functions, fscanf(), fprintf(), and freopen(), are introduced, too.