Can someone please explain me this code, this was one of the question asked in challenge. | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Can someone please explain me this code, this was one of the question asked in challenge.

union Values{ int first; int second; char third[1]; }; int main() { union Values v; v.first = 0; v.third[0] = 'b' printf("%d",v.second); return 0; } //Output : 98

16th Jun 2019, 11:17 AM
Veda Shree S
Veda Shree S - avatar
1 Respuesta
+ 3
First you have to how a union works : It works as such it will hold only one value of one of the variables inside the union function and for a short detail it can just content last variable assigned to it. So at last you can see the third variable is assigned with 'b' and that means the variable third has the value 98(as per ASCII chart) and if you try to print second variable in int form and as per the fact that it has the same address as the third variable it prints 98
16th Jun 2019, 12:34 PM
Sahil Bhakat
Sahil Bhakat - avatar