16.13 – Quiz on Unions

by subbu on October 1, 2014

Questions and Answers on Unions

1. What would be the output of following program?

#include<stdio.h>
union
{
 int x;
 char ch;
}a;
int main()
{
 a.x=45;
 printf("x=%d\n",a.x);
 return 0;
}
Show Answer
Output
x=45

A union can be defined without a name tag

2. What would be the output of following program?

#include<stdio.h>
union
{
 int x;
 char ch;
}a={12,'a'};
int main()
{
 a.x=45;
 printf("x=%d\n",a.x);
 return 0;
}
Show Answer
Output
Error

Multiple values can’t be initialized to a union variable

3. What would be the output of  following program?

#include<stdio.h>
union num
{
 int x;
 float y;
};
int main()
{
 union num a={45.90};
 printf("x=%d\n",a.x);
 return 0;
}
Show Answer
Output
x=45

Only a single value can be initialized to a union variable, which will be assigned to the first member

4. What would be the output of following program in gcc?

#include<stdio.h>
union num
{
 int x;
 float y;
};
int main()
{
 union num a={.y=45.90};
 printf("y=%d\n",a.y);
 return 0;
}
Show Answer
Output
y=45.900000

In Ubuntu Linux gcc we can explicitly assign a value to any member of union variable by specifying its name

5. What would be the output of following program?

#include<stdio.h>
union num
{
 int x;
 float y;
};
int main()
{
 union num a,*p;
 a.x=45;
 p=&a;
 printf("x=%d\n",p->x);
 return 0;
}
Show Answer
Output
x=45

6. We can create a pointer to union variable like a struct variable

#include<stdio.h>
typedef union
{
 int x;
 float y;
}nm;
int main()
{
 nm a;
 a.x=45;
 printf("x=%d\n",a.x);
 return 0;
}
Show Answer
Output
x=45

We can create an alias to the union definition

7. What would be the size of following union?

#include<stdio.h>
union data
{
 double x;
 char y[40];
};
int main()
{
 printf("Size %d Bytes",sizeof(union data));
 return 0;
}
Show Answer
Output
Size 40 Bytes

8. What would be the output of following program?

#include<stdio.h>
typedef union
{
 double x;
 char y[40];
}data;
int main()
{
 printf("Size %d Bytes",sizeof(data));
 return 0;
}
Show Answer
Output
Size 40 Bytes

9. What would be the output of following program?

#include<stdio.h>
union data
{
struct
{
short p;
float q;
}a;
struct
{
short x;
double y;
}b;
};
int main()
{
printf("Size %d Bytes",sizeof(union data));
return 0;
}
Show Answer
Output
Size 10 Bytes

Here “a”, “b” are two struct type variables out of which “b” takes more space (10 Bytes) hence allocates 10 Bytes.

10. What would be the output of following program?

#include<stdio.h>
union data
{
 struct
 {
   short p;
   float q;
 }a;
 struct
 {
   short x;
   double y;
 }b;
};
int main()
{
 union data var1,var2;

 var1.a.p=45;
 var1.a.q=70.78;
 printf("p=%d\n",var1.a.p);
 printf("q=%d\n",var1.a.q);

 var2.b.x=60;
 var2.b.y=85.90;
 printf("x=%d\n",var2.b.x);
 printf("y=%d\n",var2.b.y);
 return 0;
}
Show Answer
Output
p=45
q=0
x=60
y=-26214

var1, var2 are two different variables belong to union. In any variable we should be capable to use either of the struct variables with two values but the above program is proving that, only a single member out of two struct variables and their members can be used.

11. What would be the output of following program?

#include<stdio.h>
struct data
{
 union
 {
  short p;
  float q;
 }a;
 union
 {
  short x;
  double y;
 }b;
};
int main()
{
 struct data var;
 var.a.p=10;
 var.b.x=20;
 printf("%d\n",var.a.p);
 printf("%d\n",var.b.x);
 return 0;
}
Show Answer
Output
10
20

Union variables can be defined as struct members

Previous post:

Next post: