0

Why does this code work:

#include<stdio.h>
int main(){
 union var{
 int a, b, c;
 };
 printf("%ld", sizeof(union var));
 return 0;
}

My doubt is that isn't union var a declaration only, and during declaration no memory is allocated. So why does this code print 4?

programmer
  • 95
  • 1
  • 6
  • 5
    You are asking the code to print the size of the *type.* You can do the same with `int`, without actually declaring an `int` variable. – Beta Feb 15 '21 at 15:33
  • 1
    and you should be using the `%z` (probably `%zu`) format specifier: https://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family – yano Feb 15 '21 at 15:35
  • @Beta Does a union or structure declaration have memory associated with it? – programmer Feb 15 '21 at 15:57
  • My doubt is regarding declaration and definition @Beta – programmer Feb 15 '21 at 15:58
  • See this https://stackoverflow.com/questions/21584541/does-structure-declaration-occupies-memory – programmer Feb 15 '21 at 15:59
  • Yes, it does. What are you unsure of? – Beta Feb 15 '21 at 16:58

2 Answers2

3

It prints 4 because that's how big a union var variable is. The fact that there are no union var variables in your program, is completely irrelevant. If you created one, for example by writing union var myUnionVar;, it would use 4 bytes of memory

You can also do this with structs:

struct list_node {
    struct list_node *next;
    int key;
    int value;
};

// note that sizeof returns a size_t which should be printed with %zu
printf("%zu", sizeof(struct list_node)); // prints 12 or 16, probably
user253751
  • 57,427
  • 7
  • 48
  • 90
  • Can you please elaborate " It prints 4 because that's how big a union var variable is." – programmer Feb 15 '21 at 16:05
  • @programmer Do you know what variables are and what structs are? And how unions are kinda like structs but not really? – user253751 Feb 15 '21 at 16:05
  • Though I know the differences between structures and union, I don't see how it is relevant to this question. Anyways, please tell what particular difference you want me to know – programmer Feb 15 '21 at 16:09
  • @programmer Do you know what an int variable is? – user253751 Feb 15 '21 at 16:10
  • @programmer _how big a variable variable is_ = _the size a variable uses in memory_. An `int` typically uses 4 bytes. – Jabberwocky Feb 15 '21 at 16:12
  • Ah I got it now, union is a user defined data type just like int.. So it would print how many bytes would be occupied by a variable of that type if a variable of that type would be declared, right? – programmer Feb 15 '21 at 16:18
  • @programmer Yes. – user253751 Feb 15 '21 at 16:22
2

sizeof is an operator that yields the size in bytes of its argument (right-hand-side operand). The operand can either be a type or a variable. Most of the time, the result is a constant that can be evaluated at compile time.

union var is a type, therefore with sizeof(union var) you are asking "what size would occupy a variable if it had the type union var?" The answer is 4 bytes.

xhienne
  • 5,738
  • 1
  • 15
  • 34