C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 10)
10.
What is 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:
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.
So the output is 3, 2, 515.
Discussion:
77 comments Page 4 of 8.
Devang r dixit said:
10 years ago
Please give output with explanation:
int main()
{
union a
{
int i;
float j;
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.j,u.i);
return 0;
}
int main()
{
union a
{
int i;
float j;
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.j,u.i);
return 0;
}
Ishu said:
10 years ago
Why value of u?
I means the entire size of union? Can someone help me out?
I means the entire size of union? Can someone help me out?
Ace said:
1 decade ago
#include<stdio.h>
int main()
{
union a
{
long int i, j;
int ch[2];
};
union a u;
u.ch[0] = 3;
u.i=1;u.j = 2;
u.ch[1] = 5 ;
printf( "%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.j, u.i );
return 0;
}
int main()
{
union a
{
long int i, j;
int ch[2];
};
union a u;
u.ch[0] = 3;
u.i=1;u.j = 2;
u.ch[1] = 5 ;
printf( "%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.j, u.i );
return 0;
}
Ravali said:
1 decade ago
I am not able to understand. And @Nandini you have a good explanation but I have understood it partially can you explain t more deeply?
Kuldip said:
1 decade ago
I read all question and answer but still I can't get answer of why union share common memory to all data elements.
Bhushan said:
1 decade ago
How we can calculate the value of u(I) at memory block 512?
Komal said:
1 decade ago
ch is char type of variable then how can we assign it number?
Ritika said:
1 decade ago
Please explain in detail why is it giving 515 as the size of the union?
Dhivya said:
1 decade ago
I confused that why we have to calculate the size of the union for i. Why it is not 0 like in structure?
Moon said:
1 decade ago
Please give an example and give a description? Why we use struct and union ? where can we use it (with an example) ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers