I wrote this tiny code:
#include <stdio.h>
int main() {
size_t temp;
temp = 100;
printf("lld=%lld, ld=%ld, u=%u\n", temp, temp, temp);
return 0;
}
I am running this on a i386 GNU/Linux machine with gcc version 4.1.1 20070105 (Red Hat 4.1.1-52). This is the output that I got:
lld=429496729700, ld=100, u=7993461
I can understand that the first (lld
) was printed as garbage because the printf
tries to print 8 bytes (for signed long long
as signified by lld
) when only 4 bytes are available from variable temp
.
But, I fail to understand why the last identifier, u
is getting printed as garbage – whereas, in my understanding this is the closest applicable identifier for size_t
.
Here I have assumed that size_t
is unsigned int
(which is signed 4 bytes for my i386).
Now, I did a little tweaking with the printf
line:
...
printf("ld=%ld, u=%u, lld=%lld\n", temp, temp, temp);
...
and I have a perfectly fine answer (except the lld
part).
ld=100, u=100, lld=34331653576851556
Can someone please help me in understanding what exactly am I missing here?
Thanks a lot for any help!
[side note: I tried switching optimization using gcc -O[0,2]
tag on/off without any difference in the observation.]
Best Answer
That's because what you've pushed on the stack is three 32-bit values and your format string tries to use four of them or, more accurately, one 64-bit value and two 32-bit values.
In the first case, the
lld
sucks up two 32-bit values, theld
sucks up the third one and theu
gets whatever happens to be on the stack after that, which could really be anything.When you change the order of the format specifiers in the string, it works differently because the
ld
sucks up the first 32-bit value, theu
sucks up the second and thelld
sucks up the third plus whatever happens to be on the stack after that. That's why you're getting different values, it's a data alignment/availability issue.You can see this in action with the first value. 429496729700 is equal to
(4294967296 + 1) * 100
, i.e., (232+1)*100. Your code snippetactually has the following effect:
In the second case
the following occurs: