C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 8)
8.
Point out the error in the program?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a z1 = {512};
    union a z2 = {0, 2};
    return 0;
}
Error: invalid union declaration
Error: in Initializing z2
No error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
21 comments Page 2 of 3.

Siddharth mishra said:   1 decade ago
Union denotes singularity hence only one value at a time is permitted for the init. Hence the answer.

Uma said:   1 decade ago
In unions we can initialize only one member at the declaring of object/variable for the union.

Nikhil said:   1 decade ago
In union at a time, only one variable can be initialised.. so union a z2={0,2} is an error.

SATYAPRAKASH said:   1 decade ago
YES PRETHI your correct when you assign char var to int value it gives ASCII VALUE.

Navi said:   1 decade ago
If we want all the variables of an union to be initialized then what should we do?

Sugi said:   1 decade ago
Can anyone explain the concepts of pointer structure and union clearly?

Mustafa said:   1 decade ago
In gcc compiler its giving only warning, but program is running fine.

Ashita said:   1 decade ago
How the initialization value of z1 is correct?

Gopi said:   1 decade ago
but when i execute it shows no error

Stnj said:   9 years ago
@Bnr.

You are correct. Thank you.


Post your comments here:

Your comments will be displayed after verification.