C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}
10
20
30
0
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
83 comments Page 6 of 9.

Jkhjkh said:   1 decade ago
Here we came to know that, union allocates memory of 2 bytes for an integer.

In 1st prg we have assigned as 20 and in 2nd prg we have assigned as 10. i.e., when you print the address of both we. A and we. B the address are same, because union allocates the largest memory size for 2bytes only.

David Rajesh said:   1 decade ago
Dear All,

#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}

OutPut : 20
-----------------------------------

#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
//v.a=10;
v.b=20;
v.a=10;
printf("%d\n", v.a);
return 0;
}

OutPut : 10

Here we came to know that, union allocates memory of 2 bytes for an integer.
In 1st prg we have assigned as 20 and in 2nd prg we have assigned as 10. i.e., when you print the address of both v.a and v.b the address are same, because union allocates the largest memory size for 2bytes only.

Mohamed rhiyassuddin said:   1 decade ago
#include<stdio.h>

int main()
{
union var
{
int a, b;
};
union var v;
//v.a=10;
v.b=20;
printf("%d\n", v.a);

return 0;
}
output for the above program is :20.
can u guess? v.a is not assigned any value instead the value is printed...

Sss said:   1 decade ago
Good Explanation...

Bips said:   1 decade ago
I think it will give compile error...
cause
v.a=10;
v.b=20;
printf("%d\n", v.a);
since v.b=20 overwrites the value v.a=10
but program is trying to print the value which doesnot exist.
...
..
.
correct me if i m wrong...

Sai said:   1 decade ago
The difference between union and structure is

Structure used as prototype in creating objects with same memory, it can not compress the memory.

struct a{ int a; float b; }obj;
Here obj is having 6 bytes in memory.

Where as for union, it allocates largest memory among all datatypes defined in the union.

union a{ int a; float b;}obj;
Here obj is having 4 bytes instead of 6 Bytes.

So, union utilizes largest memory assigned to variable among all variables and it will be used for all its operations.

Smit said:   1 decade ago
Good Logic Bro.
But it also in structure?

Abhay said:   1 decade ago
If in above question a is initiallised with 20 and b with 10 then
o/p becomes 10(the last updated value is 10)..... ..!!!!

Ankita gaba said:   1 decade ago
If in above question a is initiallised with 20 and b with 10 then what will be the o/p?

Shikha said:   1 decade ago
All variable inside in the union share the same memory area so all modification done in the same memory hence answer is the last updated value i.e. 20.


Post your comments here:

Your comments will be displayed after verification.