Logo F2FInterview

C Interview Questions

Q   |   QA

Structures and Unions are used to store members of different data types.

a) Declaration:

struct {    data type member1;    data type member2;   };
union {    data type member1;    data type member2;   };

 b) Every structure member is allocated memory when a structure variable is defined. The memory equivalent to the largest item is allocated commonly for all members in union.


struct emp {  char name[5];  int age;  float sal; }; struct emp e1;

Memory allocated for structure is 5+4+4=13 bytes(assuming sizeof int is 4, float is 4, char is 1). 5 byte for name, 4 bytes for age and 4 bytes for sal.

union emp1 {  char name[5];  int age;  float sal; }; union emp1 e2;

Memory allocated to a union is equal to size of the largest member. In this case, char name[5] is the largest-sized member. Hence memory allocated to this union is 5 bytes.

c) All structure variables can be initialized at a time

struct st {  int a;  float b; }; struct st s = { .a=4, .b=10.5 };

Structure is used when all members are to be independently used in a program.

Only one union member can be initialized at a time

union un {  int a;  float b; }; union un un1 = { .a=10 };

Union is used when members of it are not required to be accessed at the same time

Union is a collection of data items of different data types.
It can hold data of only one member at a time though it has members of different data types.

If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.


union pen {  char name;  float point; };

Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type.
Union is efficient when members of it are not required to be accessed at the same time.

Major advantages of pointers are:

  1. It allows management of structures which are allocated memory dynamically.
  2. It allows passing of arrays and strings to functions more efficiently. 
  3. It makes possible to pass address of structure instead of entire structure to the functions.
  4. It makes possible to return more than one value from the function.

Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data's origin.

Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: