Let’s create two c language files,
1 2 3 4 5 6 7 |
//test.c #include <stdio.h> int main(void) { printf("%lld\n", myNumber()); } |
1 2 3 4 5 6 7 8 9 |
//func.c #include <stdio.h> long long myNumber(void) { long long hash = -3524413842735980669; printf("%lld\n", hash); return hash; } |
and compile & link them:
1 2 3 |
gcc -c test.c -o test.o gcc -c func.c -o func.o gcc test.o func.o -o test |
But after run “./test”, the result is
1 2 |
-3524413842735980669 2118164355 |
Why the result from myNumber() become different in main() function?
Let’s see the assembly language source of test.c (gcc -S test.c)
1 2 3 4 5 6 7 |
...... call myNumber movl %eax, %esi movl $.LC0, %edi movl $0, %eax call printf ...... |
It only get the 32-bits result from function ‘myNumber’ (The size of %eax register is just 32-bits, smaller than the size of “long long”). Actually, we missed the declaration of myNumber() in test.c file so it only consider the result of myNumber() as 32-bits size.
After adding the declaration of myNumber() into test.c, we could check the assembly language source has changed:
1 2 3 4 5 6 |
...... call myNumber movq %rax, %rsi movl $.LC0, %edi movl $0, %eax call printf |
(The size of %rax register is 64-bits)
And the result of running ‘./test’ is correct now.
1 2 |
-3524413842735980669 -3524413842735980669 |