C语言long类型的位数

测试程序

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <limits.h>

int main()
{
long l = LONG_MAX + 1;
printf("%lld,%lld\n",LONG_MAX,l);
return 0;
}

测试结果

在 Windows 上的测试结果

1
2147483647,2147483648

在 Linux 上的测试结果

1
9223372036854775807,-9223372036854775808

注:测试操作系统均为64位操作系统,程序也是64位程序,都使用gcc进行编译,使用默认参数进行编译

解读

显然在 x64 Windows上,long类型被定义为32位整数,相当于int。而在 x64 Linux 上,
则被定义为64位整数,相当于long long。有说法认为在32位Linux上,long是32位的,但暂未实际测试。

Author: LouisZ
Link: http://notes.louisz.top/2024/06/c-long-bits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.