2

Why output is incorrect?

I think the output should be:-

19 for the struct inside the union

19 again the same because Union reserves memory for the largest variable.

43 and the outside Struct is 43 because 19+20+4=43

#include<stdio.h>
//structure
//======================================================
struct {
    char a[20];
    int b;
    union {
        double c;
        struct {
            char d[15];
            float e;
               }x;
          }y;
       }z;
//main function
//======================================================
int main() {
printf("%u\n%u\n%u\n",sizeof(z.y.x),sizeof(z.y),sizeof(z));
return 0;
}

//Output is 20 24 48
//Assumed 19 19 43
timrau
  • 22,578
  • 4
  • 51
  • 64
AbhijeetS
  • 21
  • 2
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please use the title as a short summary of your problem while you give us the details (like actual and expected output) in the question body itself. – Some programmer dude Nov 02 '19 at 13:42
  • 1
    you're also getting undefined behavior due to using the wrong format specifier, because `sizeof` returns a `size_t` which [must be printed out using `%zu`](https://stackoverflow.com/q/940087/995714) and not `%u` – phuclv Nov 02 '19 at 15:53

0 Answers0