C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 1)
1.
What will be the output of the program ?
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Answer: Option
Explanation:
The system will allocate 2 bytes for the union.
The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.
Discussion:
40 comments Page 2 of 4.
Arjun said:
1 decade ago
I understood how 515 is printed. I don't understand Why does doing u, i print that value?
Arpeeta Halder said:
10 years ago
In the above question:
union a
{
int i;
char ch[2];
};
Here, memory is allocated for int i, i.e 2 bytes now.
u.ch[0]=3;
u.ch[1]=2;
They are stored in memory allocated for int i (as char are of 1 bytes) they are placed in each bytes of integer i, now when we print.
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
Then, first u.ch[0] is printed then u.ch[1] then u.i where u.ch[0] then u.ch[1] from right to left is stored in memory allocated for u.i is saved.
union a
{
int i;
char ch[2];
};
Here, memory is allocated for int i, i.e 2 bytes now.
u.ch[0]=3;
u.ch[1]=2;
They are stored in memory allocated for int i (as char are of 1 bytes) they are placed in each bytes of integer i, now when we print.
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
Then, first u.ch[0] is printed then u.ch[1] then u.i where u.ch[0] then u.ch[1] from right to left is stored in memory allocated for u.i is saved.
Supraja said:
7 years ago
Here can we take union u instead of union a u?
Bittu said:
9 years ago
Union uses Shared memory.
So, char[] and int i occupy the same memory location.
So, char[] and int i occupy the same memory location.
Ramkumar said:
1 decade ago
u.ch[0]='a';
u.ch[1]='b';
Can anyone explain output for this definition instead of u.ch[0]=3;
u.ch[1]=2;?
u.ch[1]='b';
Can anyone explain output for this definition instead of u.ch[0]=3;
u.ch[1]=2;?
Akshata said:
8 years ago
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
Getting ans as 3, 2, 110, -143785469.
How that 110 came? Please help
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
Getting ans as 3, 2, 110, -143785469.
How that 110 came? Please help
Oswald said:
8 years ago
But union takes the memory of maximum size variable. So, in this case, int i has 4 bytes which is greater than value of ch[2] (2 elements * sizeof(char)) = 2 bytes.
Zdd said:
8 years ago
#include<stdio.h>
int main()
{
union a
{
short int i;
char ch[2];
};
union a u;
u.ch[0]='a';
u.ch[1]='b';
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Its output is:97, 98, 25185.
if I change 'short int' to 'char',then the output is; 97, 98, 97.
int main()
{
union a
{
short int i;
char ch[2];
};
union a u;
u.ch[0]='a';
u.ch[1]='b';
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Its output is:97, 98, 25185.
if I change 'short int' to 'char',then the output is; 97, 98, 97.
Sana Shekijh said:
7 years ago
How to get ui? Please explain.
Vineeta said:
4 years ago
Please explain me, why use 8 binary bits 00000011?
Anyone explain about it.
Anyone explain about it.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers