C Language
Structure and Union Question & Answer
Question : 1
What is a structure?
Answer :
Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global.
Question : 2
What is a union?
Answer :
Union is a collection of heterogeneous data type but it uses efficient memory utilization
technique by allocating enough memory to hold the largest member. Here a single area of
memory contains values of different types at different time. A union can never be initialized.
Question : 3
What are the differences between structures and union?
Answer :
A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size. A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.
Question : 4
What are the differences between structures and arrays?
Answer :
Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.
Array
1-It is a collection of data items of same data type.
2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.
Structure
1-It is a collection of data items of different data type.
2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.
Question : 5
What are enumerations?
Answer :
They are a list of named integer-valued constants.
Example:enum color { black , orange=4, yellow, green, blue, violet };This declaration defines the symbols ―black‖, ―orange‖, ―yellow‖, etc. to have the values ―1,‖ ―4,‖ ―5,‖ … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.
Question : 6
What the advantages of using Unions?
Answer :
When the C compiler is allocating memory for unions it will always reserve enough room for the largest member.