Structure and Union 405
f=isalpha(ch);
if(f!=0)
printf ("n %c is %d type of symbol V h,Letter);
else
I
f=isdigit(ch);
if(f!= 0 )
printf ("n %c is %d type of symbol "^h,Digit);
else
printf ("n %c is %d type of symbol ^h, Other);
I
QLUELJX:
Enter, any character
= is 2 type of symbol
Explanation In the above program enum data type ctype is declared with identifiers Letter, Digit and
Other. A character is entered through the keyboard. The macros isa lp h a () and i s d i g i t ()
checks the character whether it is Letter, Digit or Other. Depending upon type of character entered
appropriate identifiers are displayed. In the output = is entered. It is neither a letter nor a digit.
Hence, it comes under other type whose enumerated value is 2. Thus, we get the type entered
character with its value.
12.11 UNION
Union is a variable, which is similar to the structure. It contains number of members like structure
but it holds only one object at a time. In the structure each member has its own memory location
whereas, members of unions have same memory locations. It can accommodate one member at a
time in a single area of storage. Union also contains members of types int, float, long, arrays, pointers
etc. It allocates fixed specific bytes of memory for 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 the union contains char, integer & long integer then the number of bytes reserved in the
memory for the union is 4 bytes. An example is illustrated below for understanding.
12.28 Write a program to find size of union and number of bytes reserved for it
n a in O
{
union r e s u lt
{
int marks;
char grade;
} ;
s tru c t res
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68