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

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

Question and Answer

    Q Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.

 

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).