sizeof with a type or variable

If the type of the variable is changed, the sizeof will not require changing if the variable is the argument, rather than the type. Regarding @icepack’s comment: the possibility or likelihood of change for type vs. variable name is not the issue. Imagine the variable name is used as the the argument to sizeof and … Read more

sizeof a union in C/C++

A union always takes up as much space as the largest member. It doesn’t matter what is currently in use. union { short x; int y; long long z; } An instance of the above union will always take at least a long long for storage. Side note: As noted by Stefano, the actual space … Read more

What is the size of void?

The type void has no size; that would be a compilation error. For the same reason you can’t do something like: void n; EDIT. To my surprise, doing sizeof(void) actually does compile in GNU C: $ echo ‘int main() { printf(“%d”, sizeof(void)); }’ | gcc -xc -w – && ./a.out 1 However, in C++ it … Read more

Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]

sizeof returns size_t which is unsigned and so -1 is being converted to a very large unsigned number. Using the right warning level would have helped here, clang with the -Wconversion or -Weverything(note this is not for production use) flags warns us: warning: implicit conversion changes signedness: ‘int’ to ‘unsigned long’ [-Wsign-conversion] if (sizeof(int) > … Read more

Sizeof string literal

sizeof(“f”) must return 2, one for the ‘f’ and one for the terminating ‘\0’. sizeof(foo) returns 4 on a 32-bit machine and 8 on a 64-bit machine because foo is a pointer. sizeof(bar) returns 2 because bar is an array of two characters, the ‘b’ and the terminating ‘\0’. The string literal has the type … Read more