编写test.c
#include <stdio.h>
int main()
{
long int a,b;
printf("sizeof int is %lu, sizeof long int is %lu\n", sizeof(int), sizeof(long int)); // the size of long int in 64bit machine is 8byte; in 32bit machine, the sizeof long int is 4byte
a = 3000*1000*1000L; // add suffix L indicate it 's a long int const, otherwise it's a int const, the result of 3000*1000*1000 will overflow
printf("%ld, %ld\n", a, 3000*1000*1000L);
b = 2000*1000*1000; // the result of 2000*1000*1000 is less than the maximum of signed int value(0x7FFFFFFF =2147483647)
printf("%ld, %ld\n", b, 2000*1000*1000L); // if there is not the 'L' suffix of 2000*1000*1000L, gcc will give a complier warning like "type not match"
}
编译 gcc -o test test.c
执行程序,结果如下:
sizeof int is 4, sizeof long int is 8
3000000000, 3000000000
2000000000, 2000000000