the size of sizeof()

The return size of sizeof() is defined to be implementation defined.

The example program illustrates the problem

int main(void)
{
        printf("%d\n", sizeof(sizeof(int)));
}

On IA64 you get

ianw@lime:~$ ./test
8

and on IA32 you get

ianw@quasar:~$ ./test
4

Implications for printf()

If you're using sizeof() as an argument to printf() then to get portable code that doesn't give warnings you need to cast the return value of sizeof(). Probably the best way is to use size_t and the corresponding %z argument to printf()

IA64wiki: SizeofSizeof (last edited 2009-12-10 03:14:01 by localhost)

Gelato@UNSW is sponsored by
the University of New South Wales National ICT Australia The Gelato Federation Hewlett-Packard Company Australian Research Council
Please contact us with any questions or comments.