C Programming - Structures, Unions, Enums - Discussion

2. 

Which of the following statements correct about the below program?

#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u1 = {512};
    union a u2 = {0, 2};
    return 0;
}
1: u2 CANNOT be initialized as shown.
2: u1 can be initialized as shown.
3: To initialize char ch[] of u2 '.' operator should be used.
4: The code causes an error 'Declaration syntax error'

[A]. 1, 2
[B]. 2, 3
[C]. 1, 2, 3
[D]. 1, 3, 4

Answer: Option C

Explanation:

No answer description available for this question.

Gunjan said: (Mar 17, 2011)  
Accord to me there will be no error message in that...
Its a request from my side please do compile it on your machine and see the output..

Gunjan said: (Mar 17, 2011)  
I compile it on online compiler and found no error message
and also on my machine having Dev -C compiler and found no error.

Sundar said: (Mar 17, 2011)  
@Gunjan

Let me make some points more clearly.

1. u2 CANNOT be initialized as shown.
This line says that,
union a u2 = {0, 2};
This type of initialization cannot be done as we expected. Because, here the value 2, cannot be assigned to ch[] of u2 without '.' operator. It may cause syntax error in Turbo C but no error GCC and as you said in Dev C.

2. u1 can be initialized as shown.
This line says that,
union a u1 = {512};
This can be done. This will asign the value 512 to u1.i;

3. To initialize char ch[] of u2 '.' operator should be used.
We have to use ('.' operator) to assing value for 'ch[]' of the union 'u2'. Example: u2.ch[0] = 'x';

4. The code causes an error 'Declaration syntax error'.
Since, we know that point-1 may cause error. But it was mentioned already. So, by fixing this (if it causes any error), the rest part of the program will not cause any error.

I have tested it in Turbo C (causes error as said in point-1), and GCC (no error).

Therefore, the given answer is correct.

Pra said: (Sep 18, 2011)  
Thank you @sundar.

Sathees said: (Oct 25, 2011)  
Need some more explanations.

RP Kumawat said: (Sep 12, 2012)  
If union value define than it flow data type sequence
Ex..union a u2={int,char}
char alway define in ''
Ex: '2'
union a u2 = {0, '2'};its right.

Hemanshu said: (Mar 30, 2013)  
Let me clear something.

In union when we declare variable and initialize the variable we can pass only one value at initialization time.

That value must me of the type of first member of union.

That's why we assign value to char ch[] neither by union a u2 = {0, "a"} nor by union a u2 = {"a"} and nor by union a u2 = "a";

We must have to use '.' (dot) operator for assign value to char ch[] like as union a u2; strcpy(u2.ch,"a");

Vicky said: (Sep 8, 2014)  
In Turbo C, it shows Declaration syntax error.
For me, 1, 2, 4 are correct.

Not sure about 3rd point.

Preethi said: (Oct 20, 2015)  
Discuss answers briefly for each questions.

Priya said: (May 2, 2020)  
Thanks all for explaining.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.