+ 2

Can anyone explain me this code snippet?

#include <stdio.h> int main() { union a{ int i; char ch[2]; }; union a u; u.ch[0] = 3; u.ch[1] = 2; printf("%d%d%d\n",u.ch[0], u.ch[1],u.i ); return 0; }

28th Sep 2019, 5:27 AM
Preity
Preity - avatar
16 Answers
+ 3
Thank you both for the help :-P
28th Sep 2019, 8:50 AM
Preity
Preity - avatar
+ 2
~ swim ~ can you help me to this code
28th Sep 2019, 5:42 AM
Preity
Preity - avatar
+ 2
Just saying what I observed from running this code a couple of times. Hex value of <u>.i always ends with two digits '0203'. But the two digits before that always changes in each run. I think the <u>.ch[0] and <u>.ch[1] explains the '0203'. The random value before the '0203' is *probably* just some random two bytes (padding) bytes used to cast the <u>.ch which is only two bytes into int type <u>.i - which is 4 byte (32bit) integer. ~ swim ~ Need your help with this one bro ✌
28th Sep 2019, 6:21 AM
Ipang
+ 2
Okay ~ swim ~ got it! Thanks for the explanation 👍
28th Sep 2019, 7:28 AM
Ipang
+ 2
~ swim ~ I'm trying and stuck by that behavior your answer is clarify that then I taught if it's going to biggest range of union that might answer is coming by this behavior. Thanks for your time and help :-)
28th Sep 2019, 9:02 AM
Preity
Preity - avatar
+ 2
Preity I modified the code by changing <ch> from two bytes into four bytes. Being a four bytes (or character) array, the values in <ch> can be converted into an int with a predictable result. But when <ch> only consists of two bytes the conversion to int results varies. Maybe this snippet can illustrate what I mean. #include <stdio.h> int main() { union a { int i; char ch[4]; //char ch[2]; }; union a u; u.ch[0] = 3; u.ch[1] = 2; u.ch[2] = 0x19; u.ch[3] = 0x20; printf("%d %d %x\n", u.ch[0], u.ch[1], u.i); // Output: 3 2 20190203 // %d %d %x // 3 2 20 19 02 03 // ch [0] [1] [3] [ 2] [1] [0] return 0; } If <ch> was only two bytes the 20 and 19 (in hex output) contains random values. Still not sure how to explain that. But I believe it has something to do with undefined behaviour as ~ swim ~ had explained earlier in this thread.
28th Sep 2019, 9:59 AM
Ipang
+ 2
~ swim ~ That's exactly what it means isn't it? like you said "In union you can get surprises ..." the surprises here is the undefined behaviour. Different things can happen and different output can occur from that thing : )
28th Sep 2019, 10:50 AM
Ipang
+ 2
Difficult to explain and convince things? let experience be their teacher, don't take it all so hard : ) Idk, maybe a demo code works better when it comes to convincing some people.
28th Sep 2019, 2:06 PM
Ipang
+ 2
Ipang yes their are many codes snippet which shows undefined behavior I'm just started programming and it's feels awesome to learn by this type of concepts but in real life coding does this concept are more in use or the things learned by experience
28th Sep 2019, 2:08 PM
Preity
Preity - avatar
+ 2
Ipang oh great it's an good way to go through out I'll try to get more on good stuffs but here are really hard to find good posts
28th Sep 2019, 2:21 PM
Preity
Preity - avatar
+ 1
Preity I'm a learner too, not a professional, I believe I am not the right person to answer the doubt about real life practices (sorry). Personally I learn also from other people's code "mistakes", and the discussions covering a problem with code have taught me more than I knew before I learned coding 😁
28th Sep 2019, 2:17 PM
Ipang
+ 1
I understand what you mean. I also hope and look forward to see a better tomorrow, where I can learn even more things from good quality posts. And I wanna Thank you too, so far your questions have brought me more knowledge too 😁
28th Sep 2019, 2:26 PM
Ipang