| I tried to use sizeof to get the size of an object, but it returned zero!

Previous | The C Language | Next

Q: | When I tried to use the sizeof operator to determine the exact size of some objects, I got zero as the result. What is wrong?
A: | You probably tried something like
    printf ("%d", sizeof (something));
The ANSI standard proposes that the sizeof operator returns a value of type size_t, which is in fact long integer in this implementation. So, the result is pushed on the stack as a long integer, but the format specifier "%d" expects an ordinary integer, so it pulls from the stack just one word, which is zero in this case. You need to write
    printf ("%ld", sizeof (something));
Alternatively, you can use a typecast to convert the result to a short integer
    printf ("%d", (short) sizeof (something));
assuming that no object would be longer that 32767 bytes.