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

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.

Palaniarjun said:   9 years ago
Hi, friends

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

Union allocates highest memory location for the data type(int,float,char) in these data type float has the highest memory so union allocate 4 bytes. And union store only one value it override the previous value,so it produces the last declaration or initialization value.

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.

Remember me!! said:   1 decade ago
Here you can see the difference:

You have to print value of 'a' before 'b' get declared to 20. Because union always display the datatype having larger memory.

#include<stdio.h>

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

getch();
return 0;
}

The reason why union is used means it hold very less memory than structure it doesn't waste memory. I believe its a good think.

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

Even if we add a third variable 'c' to the union and try to print it would output the same value =20 since it will always output the last value in such cases.

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

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.

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.

Mounisha said:   7 years ago
The union will allocate for all datatypes in the same memory location, the only structure will allocate the separate memory.

The location for each data types. So, it can initialize only one value at a time v.b=20 has replaced the value of v.a=10, So it will be stored in address space 20.
(3)

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


Post your comments here:

Your comments will be displayed after verification.