II-274 Programming Concepts
struct book
{
char name[35];
char author[35];
int pages;
} b1;
void main()
{
------------
------------
show(&b1); /* Pass by address */
print(b1); /* Pass by value */
-------------
-------------
}
void show (struct book *b2) /* Pass by address */
{
------------
------------
}
void print (struct book b2) /* Pass by value */
{
------------
------------
}
OUTPUT:
Similarly, individual structure members can be passed to a function.
13. How are user-defined data types defined?
Ans: The statement typedef is to be used while defining the new user-defined data type. The
syntax is as follows:
typedef type dataname;
where type is the datatype and dataname is the user-defined name for that type.
For example, typedef int hours;
where hours is another name for int and now we can use hours instead of int in the program as follows:
hours hrs;
14. Explain the importance of bit fields. How do bit fields save memory space?
Ans: 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 and so on.
The number of bits required for a variable is specified by non-negative integer followed by a colon.
To hold the information, we use the variables. The variables occupy minimum one byte for char and
M13_ITL-ESL4791_02_SE_C13.indd 274 12/22/2012 5:06:15 PM
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
II-276 Programming Concepts
19. Explain int86() and intdos() functions. How they used to interact with hardware?
Ans: The intdos() is used for calling DOS interrupt. The intdos() invokes a DOS function by
issuing software interrupt INT 21H.
The int86() stands for 8086 interrupt. The int86() functions invoke a BIOS functions
by issuing a software interrupt. The int86() function can be invoked with the following three
arguments.
Interrupt type number corresponding to ROM-BIOS service. Two arguments of UNION type REGS.
They are ‘x’ and ‘h’. The int86() function takes the values of input registers from one ‘REGS’ union
and returns the output registers in another.
Multiple-choice Questions
1. Which of the following keywords C supports for storing variables of different data types under one
name?
(a) class (b) struct
(c) enum (d) All of these
2. Structure is a .
(a) Collection of different data types only (b) Collection of same data types only
(c) Collection of similar as well as different data types (d) None of these
3. When we declare a structure, .
(a) It defines form of structure only
(b) It reserves space in memory only
(c) It defines form of structure as well as reserves space in memory
(d) None of these
4. The elements of structure .
(a) Are stored in contiguous memory locations
(b) Share some part of memory location
(c) Have the same location as that of the largest data type
(d) Are scattered in memory, but linked together
5. The valid syntax for structure is .
(a) struct abc{ (b) struct abc{
---- ----
---- ----
}; }a,b,c;
(c) struct{ (d) All of these are valid
----
----
}a,b,c;
6. The structure elements are accessed using .
(a) . (dot operator) (b) ->(arrow operator)
(c) Both of the above (d) None of the above
M13_ITL-ESL4791_02_SE_C13.indd 276 12/22/2012 5:06:16 PM
Structure and Union II-277
7. What is the output of the following program?
void main()
{
struct abc
{
int x;
char y;
};
struct abc a1,a2;
clrscr();
a1.x = 65;
a2.y = 'B';
a1.y = a2.y;
a2.x = a1.x;
printf("%d %d %d %d",a1.x,a2.x,a1.y,a2.y);
getch();
}
(a) 65 65 66 66 (b) A A B B
(c) Compile error (d) Runtime error
8. Which of the following are correct?
(a) We can create array of structure variable.
(b) We can make one structure variable an element of other structure.
(c) We can declare pointer variable inside structure.
(d) All of the above.
9. Union is the same as that of structure, because .
(a) Both of them are used to collect same or different data types
(b) In both cases, the elements are stored in contiguous memory locations but do not share memory
(c) In both cases, the elements share memory
(d) Both a and b
(e) None of the above
10. The size of union is the size of .
(a) The sum of sizes of total variables
(b) The mean of largest variable and smallest variable
(c) The largest variable
(d) The difference of largest variable and smallest variable
M13_ITL-ESL4791_02_SE_C13.indd 277 12/22/2012 5:06:16 PM
II-278 Programming Concepts
11. What is the output of the following program?
void main(){
union abc{
int i;
char c;
};
union abc a;
clrscr();
a.i = 256+48;
printf("%d %c",a.i,a.c);
getch();
}
(a) 256 48 (b) 256 0
(c) 304 48 (d) 304 0
12. The enum elements .
(a) Are constants (b) Are variables
(c) Are always start with 1 (d) Cannot be changed inside enum
13. Convenience and readability of “enum” can also be achieved by using .
(a) Structure (b) Union
(c) Macro (d) None of the above
14. Various members of the structure are specified .
(a) Within the couple of curly braces separated by semicolons
(b) Unions that are like structures
(c) Unions that are less frequently used in the program
(d) Unions that are used for set operations
15. Memory is reserved when .
(a) Structure is defined
(b) Structure variables are declared
(c) Ampersand (&) operator is used
16. The members of structure are .
(a) Similar or dissimilar data types
(b) Signed data types
(c) Unsigned data types
17. The structure is a .
(a) User-defined data type (b) System-defined variable (c) Keyword
18. The typedef is a .
(a) Variable (b) System-defined file (c) Keyword
19. The size of structure is decided by .
(a) Its members (b) One of its members (c) None of the above
M13_ITL-ESL4791_02_SE_C13.indd 278 12/22/2012 5:06:16 PM
..................Content has been hidden....................

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