Exercises : Answer the following Question

Submitted by tushar pramanick on Sun, 03/10/2013 - 22:03

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."

    Of the following two statements, which one is the declaration of a union and which one is the definition of union variables?

    union a_union {
         int x;
         char y;
         };

    union a_union x, y;

    What's wrong with the following union declaration?

    union automobile {
         int year;
         char model[8]
         float weight;
         }

    In the following statement, what are the values contained by the two union members?

    union u {
         int date;
         char year;
    } a_union = {1997};

    Given a structure and a union, what are the sizes of the structure and the union? (Assume that the char data type is 1 byte long and the int data type is 2 bytes long.)

    struct s {
         int x;
         char ch[4];
    } a_structure;

    union u {
         int x;
         char ch[4];
    } a_union;

Exercises

    Rewrite the program in Listing 20.1 by switching the order between the statement in line 15 and the statement in line 17. What do you get after running the rewritten program?
    Rewrite the program in Listing 20.2. This time, print out values held by all the members in the info union each time one of the members is assigned an integer.
    Write a program to ask the user to enter his or her name. Then ask the user whether he or she is a U.S. citizen. If the answer is Yes, ask the user to enter the name of the state where he or she comes from. Otherwise, ask the user to enter the name of the country he or she comes from. (You're required to use a union in your program.)
    Modify the program you wrote in exercise 3. Add a bit field and use it as a flag. If the user is a U.S. citizen, set the bit field to 1. Otherwise, set the bit field to 0. Print out the user's name and the name of country or state by checking the value of the bit field.

***

Comments

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.

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 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.