Structure and Union 413
SU MM AR Y
This chapter explains the concept of structure and programs. One of the powerful features of C language
is that it supports the creation of Structure. The various features of Structure are described at the
beginning of this chapter. For the beginners the concepts and examples on Structures are given in an
easy way and step by step process is adopted. The various titles under structure such as how the
Structures are declared, initialized, Structure within the Structure, Array of Structure, Pointer to Structure
are elaborated. Also how functions are defined in Structure are also illustrated with good examples.
The TYPEDEF facility can be used for creating user-defined data types and illustrated with many
examples. Enumerated data type, Union etc are the important subtitles of this chapter. Enumerated
data type provides user-defined data types. Union is a principal method by which the programmer
can derive dissimilar data types. The last but not least the
DOS and ROM-BIOS functions and their
applications are explained. The user is advised to go through the appendix C where by using these
functions a number of programs can be developed. If the reader executes the programs from this
chapter it will be of immense use for other applications.
EXERC ISES
A] Answ er the following questions.
1
2
3
4
5
6
8
9
10
11
12
13
14
What is a structure in C? How structure is declared?
What is the use of struct keyword? Explain the use of dot operator.
How are structure elements stored in memory?
Explain nested structure. Draw diagram to explain nested structure.
How are arrays of structure variables defined. How are they beneficial to the programmer?
How are structure elements accessed using pointer? Which operator is used?
Is it possible to pass structure variable to function? Explain in detail the possible Ways.
How are user-defined data types defined?
Explain the importance of bit fields. How do bit fields saves memory space?
Explain enumerated data type.
What is a union in C? How is data stored using union?
What are the differences between union and structure?
Explain REGS and SREGS unions. List any five CPU registers of each union type.
Explain in t 8 6 () and in tdos () functions. How are they used to interact with hardware?
B) Answer the follow ing by selecting the appropriate option.
1) Identify the most appropriate sentence to describe unions
a) unions contain members of different data types which share the same storage area in
memory
b) unions are like structures
c) unions are less frequently used in the program
d) unions are used for set operations
2) The member variable of structure are accessed by using
a) dot (.) operator b) arrow (-> ) operator
c) asterisk (*) operator d) ampersand (&) operator
414 Programming and Data Structures
3) The structure combines variables of
a) dissimilar data types b)
c) unsigned data types d)
4) The typede £ statement is used for
a) declaring user-defined data types b)
c) for typecasting of variables d)
similar data types
none of the above
declaring variant variables
none of the above
5) The number of bytes required for enumerated data type in memory are
a) 2 bytes b) 4 bytes c) 1 byte
6) The service number is always initialized in the register
a) AH b) AL c) BH
d) 3 bytes
d) AX
7) The intdos () function invokes interrupt number
a) 0x21 b)
0x17
8) The in t86 () function invokes
a) ROM-BIOS services
c) Both (a) and (b)
9) Interrupt 0x21 is a
a) softwarefnterrupt
c) both (a) and (b)
10) The union holds
a) one object at a time
c) both (a) and (b)
11) Bit fields are used only with
a) unsigned int datatype
c) char datatype
c)
b)
d)
0x18
d) none of the above
DOS services
None of the above
d)
b)
b) hardware interrupt
none of the above
b) multiple objects
d) none of the above
flo a t datatype
d) int datatype
12) Observe the following program neatly and choose the appropriate printf() statement from
the options.
struct month
{
char *month ;
};
void mainO
{
struct month m={ "March" };
. . . .
a) printf (" n Month: %s",m.month);
b) printf (" n Month: %s,,/m->month)
c) printf ( " n Month: %s",m.*month)
d) printf ("'n Month: %sVm.month)
Structure and Union 415
13) What will be the value of m displayed on execution of the following program?
#include <stdio.h>
#include cconio. h>
struct b it
{
unsigned int m : 3;
};
void main ()
{
struct b it b={8};
clrscrO ;
printf ("n m = % d,b.m);
}
a) m= 0 b) m=8
c) m=3 d) none of the above
14) The size of structure in bytes occupied in the following program will be
#include cstdio.h>
#include cconio.h>
struct b it
{
unsigned int m : 4;
unsigned int x :4;
int k;
float f;
};
struct b it b;
void main ()
{
clrscrO ;
printf ("n Size of structure in Bytes = %d",sizeof(b));
I
a) 8 b) 10 c) 7 d) 4
C] Attem pt the following program s.
1) Write a program to define structure with tag state with fields state name, number of districts and
total population. Read and display the data.
2) Write the program (l)-using pointer to structure.
3) Define a structure with tag population with fields Men and Women. Create structure within
structure using state and population structure. Read and display the data.
4) Modify the program developed using exercise (3). Create array of structure variables. Read and
display the 5 records.
416 Programming and Data Structures
5) Create user-defined data type equivalent to int. Declare three variables of its type. Perform
arithmetic operations using these variables.
6) Create enuramated data type logical with TRUE and FALSE values. Write a program to check
whether the entered number is positive or negative. If the number is positive display 0 otherwise
1. Use enumerated data type logical to display 0 and 1.
7) Write a program to accept records of the different states using array of structures. The structure
should contain char state, int population, int literacy rate and int per capita income. Assume
suitable data. Display the state whose literacy rate is highest and whose per capita income is
highest.
8) Write a program to accept records of different states using array of structures. The structure
should contain char State and number of int engineering colleges, int medical colleges, int
management colleges and int universities. Calculate total colleges and display the state, which
is having highest number of colleges.
9) Write a program to check status of the printer using int86() function. The details are as per given
below.
a) Interrupt - 0x17
b) Inputs - AH =0x02
- DX = printer port number (0=LPT1,1=LPT2,2=LPT3)
c) Returns - AH = Completion / nonsuccess code
Completion values are
Bit 0=1: time out
Bit 3=1: I/O mistake
Bit 4=1: Printer selected
Bit 5=1: Out of paper
Bit 6=1: Printer acknowledge
Bit 7=1: Printer not engaged
10) Write a program to reboot the system. Use following data with int86() function.
a) Interupt - 0x19
b) Input - Void (Nothing)
..................Content has been hidden....................

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