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 3 of 8.
Rashmi said:
9 years ago
Why u.i would print size of the union? What exactly u.i means?
Sanju said:
9 years ago
Every time I run the program, it didn't give 515 for me. It's giving some garbage value. Please help me.
Nidhi bhardwaj said:
9 years ago
But why are assuming that "i" will have size of union and not garbage value?
Because as I have studied about union, the most recent allocation is given the memory and rest all have garbage.
Because as I have studied about union, the most recent allocation is given the memory and rest all have garbage.
Kavya said:
9 years ago
How/why we get 512 and 515? Please explain me.
Punya said:
9 years ago
Please check by running the same program with c compiler, the Garbage value will be printed for i.
Rajat said:
9 years ago
When we are changing char array to short array it's not doing the same thing. In my compiler size of int is 4 and short is 2.
Akanksha said:
9 years ago
Why including only 512?
Kindly help by explaining this.
Kindly help by explaining this.
Gaurav said:
9 years ago
I think this might not be entirely correct.
Different computers use different orderings for bytes (notice I am not saying bits).
A little endian machine will place the least significant byte first (so ch[0] will come before ch[1]), and a big endian machine will do the reverse.
So the answer cannot be determined without this information.
Different computers use different orderings for bytes (notice I am not saying bits).
A little endian machine will place the least significant byte first (so ch[0] will come before ch[1]), and a big endian machine will do the reverse.
So the answer cannot be determined without this information.
Anonymous said:
10 years ago
"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." is the explanation by the indiabix.
The last words 'value of entire union size' doesn't means size of the union rather the value represented when entire union is considered as integer of 2 bytes.
Bit confusing. C stores the information in 0s and 1s,
When one say, "Hey! will you please store an int for me" for a machine with int size 2 byte will store the binary equivalent of that number in 2 bytes for example to store decimal 2 it will store 00000000 in 1st byte and 00000010 in second byte, at time of retrieval C will read 2 consecutive bytes change it into decimal and will output it for you.
When one say, "Hey! will you please store a character for me. "Machine will reserve 1 byte of memory and store ascii value of the character in that one byte. For example if character is 'a' ASCII value 65 (Binary:- 01000001) is stored in that byte. At time of retrieval the ASCII code will be changed to corresponding character.
In the case we are discussing the char1 and char0 are stored at memory location say 2000 and 2001, at time of reading (when printf ("%d", u.i) ) , an integer is read at location 2000 (yes, the beauty of union!) and C reads the 2 bytes 2000 and 2001 that are 00000011 and 00000010 c combines it to make 0000000100000011 which is binary for 515.
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." is the explanation by the indiabix.
The last words 'value of entire union size' doesn't means size of the union rather the value represented when entire union is considered as integer of 2 bytes.
Bit confusing. C stores the information in 0s and 1s,
When one say, "Hey! will you please store an int for me" for a machine with int size 2 byte will store the binary equivalent of that number in 2 bytes for example to store decimal 2 it will store 00000000 in 1st byte and 00000010 in second byte, at time of retrieval C will read 2 consecutive bytes change it into decimal and will output it for you.
When one say, "Hey! will you please store a character for me. "Machine will reserve 1 byte of memory and store ascii value of the character in that one byte. For example if character is 'a' ASCII value 65 (Binary:- 01000001) is stored in that byte. At time of retrieval the ASCII code will be changed to corresponding character.
In the case we are discussing the char1 and char0 are stored at memory location say 2000 and 2001, at time of reading (when printf ("%d", u.i) ) , an integer is read at location 2000 (yes, the beauty of union!) and C reads the 2 bytes 2000 and 2001 that are 00000011 and 00000010 c combines it to make 0000000100000011 which is binary for 515.
(1)
Jenifer said:
10 years ago
How does the statement printf("%d %d %d", u.ch[0],u.ch[1],u.i); prints the size of union?
Any variable hold some garbage value without initialization.
Here we din't used sizeof operator too then how its calculating the size and printing. Any one please explain?
Any variable hold some garbage value without initialization.
Here we din't used sizeof operator too then how its calculating the size and printing. Any one please explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers