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;
}
Discussion:
21 comments Page 1 of 3.
Aishwarya said:
4 years ago
The difference between structure and union is the size of the structure is the sum of the size of its data members, whereas the size of the union is the size of the largest element.
All members of the union share the same memory space.
size of union = size of largest datatype element
So here the size of the union is 4 bytes and shared by integer and char array, so redeclaration should overwrite the values.
union a z2 = {0, 2}; // gives warning: excess elements in union initializer.
printf("%d\n",z1);// if we print z1 it prints 512.
z1.i =16961; / / redeclare z1 will overwrite its value because its a union.
z1.ch; / / array will store the same value but split it bitwise.
0100 0010 0100 0001 =16961 (16384 + 512 + 64 +1)
0100 0010 =66(64+2) 0100 0001=65(64+1)
B (ch[1]) A (ch[0])
printf("%d\n",z1); //now if we print z1 it prints 16961.
printf("%c %c\n",z1.ch[0],z1.ch[1]); // it prints A B.
This is how the union works as per my knowledge.
All members of the union share the same memory space.
size of union = size of largest datatype element
So here the size of the union is 4 bytes and shared by integer and char array, so redeclaration should overwrite the values.
union a z2 = {0, 2}; // gives warning: excess elements in union initializer.
printf("%d\n",z1);// if we print z1 it prints 512.
z1.i =16961; / / redeclare z1 will overwrite its value because its a union.
z1.ch; / / array will store the same value but split it bitwise.
0100 0010 0100 0001 =16961 (16384 + 512 + 64 +1)
0100 0010 =66(64+2) 0100 0001=65(64+1)
B (ch[1]) A (ch[0])
printf("%d\n",z1); //now if we print z1 it prints 16961.
printf("%c %c\n",z1.ch[0],z1.ch[1]); // it prints A B.
This is how the union works as per my knowledge.
(3)
Wikiok said:
1 decade ago
Only the 1st variable can be initialized in standard C. But there are some compilers that allows other members initialization:
union a z2 = { .ch[0]=1; .ch[1]=2; };
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fstrin.htm
union a z2 = { .ch[0]=1; .ch[1]=2; };
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fstrin.htm
Bnr said:
9 years ago
All of you read this here. Union is not a problem, in this union has two members one is 'int' and another is 'char' all of you may know how to initialize character must be placed in between two double quotes hear z2 character initialization is error correct is '2' this.
(1)
Pradeep said:
1 decade ago
Yes, in union we can create only one object or variable at a time whereas in structure only we can create any number of variables or object. So option (B) is correct. In this program z2 is second variable or object created so it is not possible in union.
Ing said:
8 years ago
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a z1 = {512};
union a z2[2] = {5,6};
return 0;
}
int main()
{
union a
{
int i;
char ch[2];
};
union a z1 = {512};
union a z2[2] = {5,6};
return 0;
}
Swati said:
1 decade ago
When I execute this program it show.
No errors or program output.
But option provides right answer Error: in Initializing z2.
Which is best answer in both two?
No errors or program output.
But option provides right answer Error: in Initializing z2.
Which is best answer in both two?
Ashwani said:
1 decade ago
@Preethi:: U R correct but ur answer comes in 2nd the priority list after Nikhil's answer...So i think Nikhil's answer is more appropriate to this question..
Sudha said:
1 decade ago
Union allocates at only once time for memory allocation at the same type of union and overwrite allocate in one value.
Sexy said:
7 years ago
Program compiles normally but issues a warning of trying to assign more than one value at a time for a union.
(1)
Preethi said:
1 decade ago
I guess the variable ch is declared to be as character but int values are assigned to it. Am I correct?.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers