CPDS Unit 2 QB
CPDS Unit 2 QB
PART A
1. What is a Structure in C?
Structure is a user-defined datatype in C language which allows us to combine
data of different types together. Structure helps to construct a complex data type
which is more meaningful.
In structure, data is stored in form of records.
2. How to define a Structure?
struct keyword is used to define a structure. struct defines a new data type
which is a collection of primary and derived data types.
Syntax
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
3. What is Union?
A union is a special data type available in C that allows to store different data
types in the same memory location.
You can define a union with many members, but only one member can contain a
value at any given time. Unions provide an efficient way of using the same
memory location for multiple purpose.
4. Give the syntax for creating a union.
union [union name]
{
member definition;
member definition;
...
member definition;
};
5. Difference between Structure and Union.
Structure Union
The Keyword struct is used to define The Keyword union is used to define
the Structure the Union
Structure allocates storage space for all Union allocates one storage space for
its members seperately. all its members.
Structure occupies high memory space Union occupies low memory space
when compared to Structure
We can access all members of Only one member of union can be
Structure at a time accessed at a time.
Altering the value of a member will Altering the value of a member will
not affect other member of a structure alter other member value in union.
User can only use the function but User can use this type of function.
cannot change (or) modify this function. User can also modify this function.