C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Point Out Correct Statements (Q.No. 2)
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' |
Discussion:
10 comments Page 1 of 1.
Priya said:
5 years ago
Thanks all for explaining.
Preethi said:
10 years ago
Discuss answers briefly for each questions.
Vicky said:
1 decade ago
In Turbo C, it shows Declaration syntax error.
For me, 1, 2, 4 are correct.
Not sure about 3rd point.
For me, 1, 2, 4 are correct.
Not sure about 3rd point.
Hemanshu said:
1 decade ago
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");
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");
RP kumawat said:
1 decade ago
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.
Ex..union a u2={int,char}
char alway define in ''
Ex: '2'
union a u2 = {0, '2'};its right.
Sathees said:
1 decade ago
Need some more explanations.
Pra said:
1 decade ago
Thank you @sundar.
Sundar said:
1 decade ago
@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.
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.
(2)
Gunjan said:
1 decade ago
I compile it on online compiler and found no error message
and also on my machine having Dev -C compiler and found no error.
and also on my machine having Dev -C compiler and found no error.
Gunjan said:
1 decade ago
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..
Its a request from my side please do compile it on your machine and see the output..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers