You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

64位Windows下GCC中int和long均为4字节的原因咨询

C语言标准仅对整数类型的最小取值范围和大小相对关系做了强制约束,并没有要求short、int、long三种类型的大小必须完全不同:

  • 法定大小关系为 sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)
  • 各类型最小长度要求:short≥2字节,int≥2字节,long≥4字节,long long≥8字节

你遇到的long和int同为4字节的情况,是Windows平台特有的数据模型约定导致的:

  • 64位Linux、macOS等系统采用LP64模型,其中int为4字节,long为8字节,指针为8字节
  • 全版本Windows(包括32位、64位)都采用LLP64模型,其中int为4字节,long为4字节,只有long long为8字节,指针为8字节

你使用的MSYS2 GCC默认适配Windows原生的ABI规则,所以遵循LLP64模型的约定,输出的long大小为4字节是完全符合规范的。如果需要跨平台使用固定长度的整数类型,可以引入<stdint.h>头文件,使用int32_t、int64_t这类明确指定位数的类型即可。

测试代码

#include <stdio.h>

int main()
{
 printf("Size of short is %d",sizeof(short));
 printf("\nSize of int  is %d",sizeof(int));
 printf("\nSize of long  is %d",sizeof(long));
}

运行输出

Size of short is 2
Size of int  is 4
Size of long  is 4

内容的提问来源于stack exchange,提问作者novice programmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 15:15:02