So unions are a fantastic feature built into C and what they allowed to do is they allow you to share
memory regions with multiple variables essentially by changing either one of these variables will affect
them all because they all share the same memory region.
There are many reasons why you would use unions particularly in low level code so less now credit union.
I'll explain as we go along.
Union.
My union put a semicolon at the end.
Instead a sharpie so I went here to Union my union.
ABC an hour ago ABC DUG A equals fifty and then to ABC they'll be equals twenty and I want us to output
a and b.
So we're gonna go and do that now
and what you're now going to see is if we compile lab and run that you're going to see 20 20.
Now why is that.
Because both variables in here share the same memory region.
So when we set into a 50 and then set choppy to 20 we overwrite the memory in variable a because they
are sharing the same memory.
So that is a fantastic feature.
If I was to do something like 0 x f f f f f f f f and this is hexadecimal by the way this is how you
do hexadecimal and C and then in here we we keep that is 20.
If you now change these to X and X This will print out hex hexadecimal values for us.
If we now compile that again and we run that you don't see f f f f f f f you see f f f f f f f 14 and
then the hex equivalent of Shelby is 14.
So by getting rid of this that by by getting rid of this completely removing it and we compile that
again you now see that both A and B are equal to f f f f f f f because they share the same memory region.
So you can also define unions and structures as I've done here and you'll notice that we can now do
SDR A equals 50 s top b equals 20 and that will perform exactly the same way as I just showed you and
obviously above the union here you can have other variables such as Z whose memory location is not shared.