Structure and Union 397
Record N o.: 2
First Name : Vijay
Middle Name : Kamal
Last Name : Nandedkar
City & Pincode : Nanded - 431 602
Record No. : 1
First Name : Jay
Middle Name : Mohan
Last Name : Deshmukh
City & Pincode : Nanded - 431 602
Record No. : 2
First Name Vijay
Middle Name : Kamal
Last Name Nandedkar
City & Pincode : Nanded - 431 602
Explanation In the above program the typede f structure contains employee details like first, middle,
last name, city & pincode. Based on this structure the user defines data type name & which is further
used in the program to accept and print the relevant data fed by the user.
12.9 BITFIELDS
Bit field provides exact amount of bits required for storage of values. If a variable value is 1 or 0 we need
a single bit to store it. In the same way if the variable is expressed between 0 and 3, then the two bits are
sufficient for storing these values. Similarly if a variable assumes values between 0 and 7 then three
bits will be sufficient to hold the variable and so on. The number of bits required for a variable is
specified by non-negative integer followed by colon.
To hold the information we use the variables. The variables occupy a minimum of one byte for
char and two bytes for integer. Instead of using complete integer if bits are used, space of memory
can be saved. For example, to know the information about the vehicles, following information has to
be stored in the memory.
1. Petrol Vehicle
2. Diesel Vehicle
3. Two Wheeler Vehicle
4. Four Wheeler Vehicle
5. Old Model
6. New. Model
In order to store the status of the above information we may need two bits for types of fuel as to
whether the vehicle is of petrol or diesel type. Three bits for its type as to whether the vehicle is two
or four-wheeler. Similarly, three bits for model of the vehicle. Total bits required for storing the information
would be 8 bits i.e. 1 byte. It means that the total information can be packed into a single byte. Eventually
bit fields are used for conserving the memory. The amount of memory saved by using bit fields will be
substantial which is proved from the above example.
However there are restrictions on bit fields when arrays are used. Arrays of bit fields are not
permitted. Also the pointer cannot be used for addressing the bit field directly, although the use of