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 4 of 9.

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...

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.

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.

Srihari said:   1 decade ago
Union allocate memory for the variables which has highest.

Size only once, and all other variables are over write in it.

Hence the above program memory for int allocate once, first it store 'a' variable value later it over write with recent initialize 'b'.

Value, so the out put will be 'b' value.

Prashanth said:   1 decade ago
David Rajesh your explanation is good

Spurthi said:   1 decade ago
Answer is 20 because in union if we enter two values then the first value is replaced by another value so here 10 is replaced by 20.

Sathya said:   1 decade ago
How it will print 20? I can't understand please explain it.

Atul kumar said:   1 decade ago
Union allocates single memory for all variables, which has the highest size. So first value(10) will be overwrite with second value(20) because memory location is one. Hence output will be 20.

Sandy said:   1 decade ago
What if the data types are different in union?

ex: union var{
int a;
double b;
}

Nalu said:   1 decade ago
When the data types are different the highest datatype size will be assigned to union and only one variable can be accessed at a time. And it overwrites the same location again and again as long as you use the variables of union. !:).


Post your comments here:

Your comments will be displayed after verification.