Structure and Union II-275
two bytes for int. Instead of using complete integer if bits are used, the space of memory can be
saved.
For example,
struct vehicle
{
unsigned type: 3; /* Three bits */
unsigned fuel: 2; /* Two bits */
unsigned model: 3; /* Three bits */
};
15. Explain the enumerated data type.
Ans: The enum is a keyword. It is used for declaring enumeration types. The programmer can create
their own data type and define what values the variables of these data types can hold. This enumeration
data type helps in reading the program.
Consider the example of 12 months of a year.
enum month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
This statement creates a user-defined data type. The keyword enum is followed by the tag name
month. The enumerators are the identifiers Jan, Feb, Mar, Apr, May and so on. Their values
are constant unsigned integers and starts from 0. The identifier Jan refers to 0, Feb to 1 and so on.
16. What is a union in C? How is data stored using union?
Ans: Union is a variable, which is similar to the structure. Union contains a number of members, but
it holds only one object at a time with a common memory location for all objects.
It allocates fixed specific bytes of memory for the access of data types irrespective of any data type.
The union requires bytes that are equal to the number of bytes required for the largest members.
For example, if the union contains char, integer and long integer, then the number of bytes
reserved in the memory for the union is 4 bytes.
17. What are the differences between union and structure?
Union Structure
Holds one object at a time. Holds many objects at a time.
Members have a common shared memory space. Each member has its own memory location.
Size of union is equal to the maximum memory required
by a member of a union.
Size of structure is equal to the total memory required for
all its members.
18. Explain REGS and SREGS unions. List any five CPU registers of each union type.
Ans: The C language defines a union type called ‘REGS’ and structure type called ‘SREGS’ in the
file ‘DOS.H’, which escort the C compiler. These data types are used to pass parameters to the CPU
registers.
The union type ‘REGS’ defines AX, BX, CX, DX and SI registers. These registers are used for holding
the data temporarily. The structure type ‘REGS’ defines AX, BX, CX, DX and SI registers. The structure
type ‘SREGS’ defines the segment registers DS, ES, CS and SS. REGS and SREGS are used to invoke
the DOS functions.
M13_ITL-ESL4791_02_SE_C13.indd 275 12/22/2012 5:06:15 PM