Page 131 - 6437
P. 131

interpreted. The type may be int, signed int, or unsigned int.



                                         member_na          The name of the bit-field.
                                 me

                                         width              The number of bits in the bit-field. The width must be less than or
                                                    equal to the bit width of the specified type.

                          The variables defined with a predefined width are called bit fields. A bit field can hold
                   more than a single bit; for example, if you need a variable to store a value from 0 to 7, then you
                   can define a bit-field with a width of 3 bits as follows:
                          The above structure definition instructs the C compiler that the age variable is going to

                 struct
                 {
                   unsigned int age : 3;

                 } Age;

                   use only 3 bits to store the value. If you try to use more than 3 bits, then it will not allow you to do
                   so. Let us try the following example:
                            #include <stdio.h>
                            #include <string.h>


                            struct

                            {
                              unsigned int age : 3;

                            } Age;


                            int main( )
                            {

                                Age.age = 4;
                                printf( "Sizeof( Age ) : %d\n", sizeof(Age) );

                                printf( "Age.age : %d\n", Age.age );


                                Age.age = 7;
                                printf( "Age.age : %d\n", Age.age );


                                Age.age = 8;



                                                                                                     134
   126   127   128   129   130   131   132   133   134   135   136