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 continue Statement

The continue Statement

The break Statement

The break Statement

You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.

The switch Statement

The switch Statement

Nested if Statements

Nested if Statements

As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.

The if-else Statement

The if-else Statement