1

The variable a is assigned by value 10 and the variable b is assigned by 20 with the union variable v. Then it gives the output of a is 20 instead of 10. I don't get it.

#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;
}  

I executed the program and I got 20 as output.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Vaibhav
  • 136
  • 1
  • 3
  • 13
  • Not sure what you're asking; but [Google](https://www.google.com/search?q=c+union) has plenty of info on C unions –  Aug 19 '13 at 04:11
  • Thanks @JensGustedt . I've Deleted misleading comment (you're only supposed to read the field which was most recently written) – Himanshu Aug 19 '13 at 06:33

6 Answers6

6

union overlays all the listed members on top of each other (although some may extend farther than the overlapping initial sections), so assigning to either .a or .b is writing over the same part of memory. With that in mind, the output should make sense.

You probably were thinking of struct's behavior if you were expecting an output of 10.

In really twisted scenarios, it's possible to have parts of different values stored in a union simultaneously, but generally the partially overwritten values will be assumed corrupt. For example, this:

union {
    char a;
    struct { char ba; char bb; } b;
} s;

can store s.a and s.b.bb at the same time, but since s.a overlaps s.b.ba, assigning to s.a stomps on s.b.ba, and by implication all of s.b is no longer trustworthy.

Often unions intended to store different types will be embedded in a struct whose first member records which union member is in use:

struct {
    int type;
    union {
       char ch;
       int n;
    } datum;
} atom;

Here, type probably would contain an enumerated value to indicate whether datum.ch or datum.n was in use in the atom.

Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
2

In union at a time you can assign one value. In union the values are overlapped with each other. So you are doing the wrong with assigning values to two elements of union. It will always show the last assigned value. So if you are accessing the value of a it will show the last updated value.

1

This is an instructive question. Try reversing the assignments, as:

   v.b=20;
   v.a=10;

And see the result. If you want a and b to co-exist rather than overlay each other then use a structure, as:

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

and you should get the result want. However, the var structure now contains two integers.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
0

A union differs from a struct in these two ways:

  • Space is allocated for the largest type present (since they're both int, the size of the union overall is int)
  • Only one value may be stored in a union at any time.

The last value you store into v is 20, so one should expect that value back when printing any member of the union.

If you wanted to have access to both members, then you would want to use a struct instead.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • If I want the values a=10 and b=20 then can I use the union..?? How i can get these values using union, whether it is possible or i should use structure..?? – Vaibhav Aug 19 '13 at 04:18
  • **No.** Use `struct` if you want that behavior. – Makoto Aug 19 '13 at 04:19
  • @Vaibhav Why are you trying to use a union when you have no idea what it is or what it's for? Why *wouldn't* you use a struct? – Jim Balter Aug 19 '13 at 04:24
  • @JimBalter: There are cases...some cases which some would consider "undefined" or "dirty, evil hacks"...that a `union` can come in handy. In *this* particular case, however, I absolutely agree with you. – Makoto Aug 19 '13 at 04:24
  • Your response has no bearing on what I wrote; I said nothing about unions not being handy ... and it doesn't require anything undefined or dirty. – Jim Balter Aug 19 '13 at 05:20
0

You should read about union:

This will help you understand what happens - basically all members of a union are stored at the same address in memory.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
0

Lets assume, a primitive int size is 32 bits.

The concept of union data type in C/C++ is: it enables us to store different data types in the same memory location. We can define a union with many members, but only one member can contain a value at any given time. The size of a union is sufficient to contain the largest of it's members.

In your case: both 'a' & 'b' is int type, so the size of the 'var' union is 32 bits.

Now imagine binary array of 32 in size. Lets say it A[32]. The least 5 significant bits store 01010, i.e. binary form of 10 which is assigned to 'v.a'. Next when you assigned 20 to 'v.b', then 10100 (binary form of 20) will replace 01010. So currently the union v having 10100 in it's memory space.

When you are accessing the 'v.a', it is returning the current value of that memory location.

See the link wikipedia link

noNameYet
  • 635
  • 2
  • 10
  • 15