我的系统是Linux x86_64。 INT数据类型的最大值显示为2147483648(使用标头)而不是32767(与16位int的情况一样)。为什么呢?
答案 0 :(得分:10)
除非您自己定义,否则没有类型INT
。该类型称为int
。 C区分大小写。
C标准表示int
类型的对象具有执行环境体系结构所建议的自然大小“。对于64位系统,这确实意味着INT_MAX
应该是2 63 -1 - 但这不是一个硬性要求。
要求是int
必须至少为16位宽,并且必须至少与short
一样宽且不宽于long
。 (POSIX要求int
至少为32位。)
为系统支持的所有大小提供整数类型很有用。特别是,在大多数现代系统中,预定义的大小为8,16,32和64位的整数类型很有用。
char
通常为8位。如果我们使int
为64位,则short
为16位且我们没有32位类型,或short
为32位且我们没有16位类型。 (我还研究过没有16位或 32位整数类型的系统。)
编译器可以通过定义自己的扩展整数类型来解决这个问题,但编译器通常不会这样做。
实际上,在64位系统上制作int
32位并不是一个真正的问题。对32位整数的操作是有效的,如果需要64位整数,可以使用long
中定义的long long
,int64_t
或<stdint.h>
。 (实际上,64位Windows将long
定义为32位,但long long
始终至少为64位。)
结论:C标准允许在定义int
方面具有相当大的灵活性,编译器实现者以他们认为最方便用户的方式定义它。与为32位系统编写的代码的兼容性通常是主要考虑因素。
答案 1 :(得分:4)
不保证各种整数类型的大小。你唯一可以指望的是:
sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short) >= sizeof(char) == 1
那就是说,在现今的大多数实际系统中,你有:
sizeof(long long) == 8
sizeof(long) == 8 or 4, depending on the architecture and compiler
sizeof(int) == 4
sizeof(short) == 2
sizeof(char) == 1
int很长一段时间没有16位。它很少是64位。除了在MSVC上,通常long与体系结构的大小相同。
您还可以使用INT_MIN / INT_MAX和limits.h中的类似常量获取其最小值/最大值。
答案 2 :(得分:0)
因为32位可以表示0到2147483647之间的整数值,16位可以表示0到32767.
您的系统似乎sizeof(int) = 4 bytes
INT_MAX = 2147483647
PS:sizeof(int)
因平台而异。
答案 3 :(得分:0)
标准定义的int
最小为16位。 2^31 = 2147483648
(遗漏了符号位),因此您的实现具有比最低要求更大的int
。
int甚至可以是更大的类型,因为标准只定义了与其他整数类型的最小长度和相对大小。
答案 4 :(得分:0)
int
的最小宽度按标准定义为16位,但现在这种情况并不常见。
相反,32位int
很常见,您的平台似乎也是如此,因此INT_MAX
可能是2147483647
。
答案 5 :(得分:0)
我刚刚在Linux Debian x64上编译了该程序:
#include <stdio.h>
int main(){
// C18 standard, p. 20
printf("\n\n***********************************************************************************\n\n");
printf ("%-25s %20s %20s %15s\n\n", "", "IZVORNA KODA", "BINARNA KODA", "OPOMBA");
signed char n009 = 255;
printf ("%-25s %20s %20d %15s\n","signed char:", "255", n009, "!");
signed char n007 = 128;
printf ("%-25s %20s %20d %15s\n","signed char:", "128", n007, "!");
signed char n006 = 127;
printf ("%-25s %20s %20d %15s\n","signed char:", "127", n006, "MAX - C18");
signed char n005 = 1;
printf ("%-25s %20s %20d %15s\n","signed char:", "1", n005, "C18");
signed char n004 = 0;
printf ("%-25s %20s %20d %15s\n","signed char:", "0", n004, "C18");
signed char n003 = -1;
printf ("%-25s %20s %20d %15s\n", "signed char:", "-1", n003, "C18");
signed char n002 = -127;
printf ("%-25s %20s %20d %15s\n", "signed char", "-127", n002, "C18");
signed char n001 = -128;
printf ("%-25s %20s %20d %15s\n", "signed char", "-128", n001, "MIN - C18");
printf("\nALOKACIJA: %d bitov",sizeof(n009)*8);
printf("\n\n***********************************************************************************\n\n");
printf ("%-25s %20s %20s %15s\n\n", "", "IZVORNA KODA", "BINARNA KODA", "OPOMBA");
unsigned char n017 = 255;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "255", n017, "MAX - C18");
unsigned char n016 = 128;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "128", n016, "C18");
unsigned char n015 = 127;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "127", n015, "C18");
unsigned char n014 = 1;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "1", n014, "C18");
unsigned char n013 = 0;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "0", n013, "MIN - C18");
unsigned char n012 = -1;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "-1", n012, "!");
unsigned char n011 = -127;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "-127", n011, "!");
unsigned char n010 = -128;
printf ("%-25s %20s %20d %15s\n","unsigned char:", "-128", n010, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n010)*8);
printf("\n\n***********************************************************************************\n\n");
short int n018 = 65535;
printf ("%-25s %20s %20d %15s\n","short int:", "65535", n018, "!");
short int n019 = 32768;
printf ("%-25s %20s %20d %15s\n","short int:", "32768", n019, "!");
short int n020 = 32767;
printf ("%-25s %20s %20d %15s\n","short int:", "32767", n020, "MAX - C18");
short int n021 = 32766;
printf ("%-25s %20s %20d %15s\n","short int:", "32766", n021, "C18");
short int n022 = 1;
printf ("%-25s %20s %20d %15s\n","short int:", "1", n022, "C18");
short int n023 = 0;
printf ("%-25s %20s %20d %15s\n","short int:", "0", n023, "C18");
short int n024 = -1;
printf ("%-25s %20s %20d %15s\n","short int:", "-1", n024, "C18");
short int n025 = -32767;
printf ("%-25s %20s %20d %15s\n","short int:", "-32767", n025, "C18");
short int n026 = -32768;
printf ("%-25s %20s %20d %15s\n","short int:", "-32768", n026, "MIN - C18");
printf("\nALOKACIJA: %d bitov",sizeof(n026)*8);
printf("\n\n***********************************************************************************\n\n");
unsigned short int n027 = 65535;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "65535", n027, "MAX - C18");
unsigned short int n028 = 32768;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "32768", n028, "C18");
unsigned short int n029 = 32767;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "32767", n029, "C18");
unsigned short int n030 = 32766;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "32766", n030, "C18");
unsigned short int n031 = 1;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "1", n031, "C18");
unsigned short int n032 = 0;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "0", n032, "MIN - C18");
unsigned short int n033 = -1;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "-1", n033, "!");
unsigned short int n034 = -32767;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "-32767", n034, "!");
unsigned short int n035 = -32768;
printf ("%-25s %20s %20d %15s\n","unsigned short int:", "-32768", n035, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n035)*8);
printf("\n\n***********************************************************************************\n\n");
int n036 = 4294967295;
printf ("%-25s %20s %20d %15s\n","int:", "4294967295", n036, "!");
int n037 = 2147483648;
printf ("%-25s %20s %20d %15s\n","int:", "2147483648", n037, "!");
int n038 = 2147483647;
printf ("%-25s %20s %20d %15s\n","int:", "2147483647", n038, "MAX - C18");
int n039 = 1;
printf ("%-25s %20s %20d %15s\n","int:", "1", n039, "C18");
int n040 = 0;
printf ("%-25s %20s %20d %15s\n","int:", "0", n040, "C18");
int n041 = -1;
printf ("%-25s %20s %20d %15s\n","int:", "-1", n041, "C18");
int n042 = -2147483647;
printf ("%-25s %20s %20d %15s\n","int:", "-2147483647", n042, "C18");
int nnnm = -2147483648;
printf ("%-25s %20s %20d %15s\n","int:", "-2147483648", nnnm, "MIN - C18");
printf("\nALOKACIJA: %d bitov",sizeof(n042)*8);
printf("\n\n***********************************************************************************\n\n");
unsigned int n043 = 4294967295;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "4294967295", n043, "!");
unsigned int n044 = 4294967294;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "4294967294", n044, "!");
unsigned int n045 = 2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "2147483648", n045, "!");
unsigned int n046 = 2147483647;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "2147483647", n046, "MAX - C18");
unsigned int n047 = 1;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "1", n047, "C18");
unsigned int n048 = 0;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "0", n048, "MIN - C18");
unsigned int n049 = -1;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "-1", n049, "?");
unsigned int n05d = -2147483647;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "-2147483647", n05d, "?");
unsigned int n050 = -2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned int:", "-2147483648", n050, "?");
printf("\nALOKACIJA: %d bitov",sizeof(n050)*8);
printf("\n\n***********************************************************************************\n\n");
long int n051 = 9223372036854775807;
printf ("%-25s %20s %20d %15s\n","long int:", "9223372036854775807", n051, "!");
long int n052 = 4294967294;
printf ("%-25s %20s %20d %15s\n","long int:", "4294967294", n052, "!");
long int n053 = 2147483648;
printf ("%-25s %20s %20d %15s\n","long int:", "2147483648", n053, "!");
long int n054 = 2147483647;
printf ("%-25s %20s %20d %15s\n","long int:", "2147483647", n054, "MAX - C18");
long int n055 = 1;
printf ("%-25s %20s %20d %15s\n","long int:", "1", n055, "C18");
long int n056 = 0;
printf ("%-25s %20s %20d %15s\n","long int:", "0", n056, "C18");
long int n057 = -1;
printf ("%-25s %20s %20d %15s\n","long int:", "-1", n057, "C18");
long int naaa = -2147483647;
printf ("%-25s %20s %20d %15s\n","long int:", "-2147483647", naaa, "MIN - C18");
long int n058 = -2147483648;
printf ("%-25s %20s %20d %15s\n","long int:", "-2147483648", n058, "?");
long int nnnn = -2147483649;
printf ("%-25s %20s %20d %15s\n","long int:", "-2147483649", nnnn, "!");
long int nbbb = -4294967294;
printf ("%-25s %20s %20d %15s\n","long int:", "-4294967294", nbbb, "!");
long int nccc = -9223372036854775807;
printf ("%-25s %20s %20d %15s\n","long int:", "-9223372036854775807", nccc, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n057)*8);
printf("\n\n***********************************************************************************\n\n");
unsigned long int n059 = 9223372036854775807;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "9223372036854775807", n059, "!");
unsigned long int n060 = 4294967294;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "4294967294", n060, "!");
unsigned long int n061 = 2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "2147483648", n061, "!");
unsigned long int n062 = 2147483647;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "2147483647", n062, "MAX - C18");
unsigned long int n063 = 1;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "1", n063, "C18");
unsigned long int n064 = 0;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "0", n064, "MIN - C18");
unsigned long int n065 = -1;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "-1", n065, "?");
unsigned long int n066 = -2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "-2147483648", n066, "?");
unsigned long int nzzz = -4294967294;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "-4294967294", nzzz, "!");
unsigned long int nggg = -9223372036854775807;
printf ("%-25s %20s %20d %15s\n","unsigned long int:", "-9223372036854775807", nggg, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n064)*8);
printf("\n\n***********************************************************************************\n\n");
long long int n080 = 9223372036854775807;
printf ("%-25s %20s %20d %15s\n","long long int:", "9223372036854775807", n080, "!");
long long int n081 = 4294967294;
printf ("%-25s %20s %20d %15s\n","long long int:", "4294967294", n081, "!");
long long int n082 = 2147483648;
printf ("%-25s %20s %20d %15s\n","long long int:", "2147483648", n082, "!");
long long int n083 = 2147483647;
printf ("%-25s %20s %20d %15s\n","long long int:", "2147483647", n083, "MAX - C18");
long long int n084 = 1;
printf ("%-25s %20s %20d %15s\n","long long int:", "1", n084, "C18");
long long int n085 = 0;
printf ("%-25s %20s %20d %15s\n","long long int:", "0", n085, "MIN - C18");
long long int n086 = -1;
printf ("%-25s %20s %20d %15s\n","long long int:", "-1", n086, "?");
long long int n087 = -2147483648;
printf ("%-25s %20s %20d %15s\n","long long int:", "-2147483648", n087, "?");
long long int n088 = -4294967294;
printf ("%-25s %20s %20d %15s\n","long long int:", "-4294967294", n088, "!");
long long int n089 = -9223372036854775807;
printf ("%-25s %20s %20d %15s\n","long long int:", "-9223372036854775807", n089, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n089)*8);
printf("\n\n***********************************************************************************\n\n");
unsigned long long int n067 = 9223372036854775807;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "9223372036854775807", n067, "!");
unsigned long long int n068 = 4294967294;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "4294967294", n068, "!");
unsigned long long int n069 = 2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "2147483648", n069, "!");
unsigned long long int n070 = 2147483647;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "2147483647", n070, "MAX - C18");
unsigned long long int n071 = 1;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "1", n071, "C18");
unsigned long long int n072 = 0;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "0", n072, "MIN - C18");
unsigned long long int n073 = -1;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "-1", n073, "?");
unsigned long long int n074 = -2147483648;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "-2147483648", n074, "?");
unsigned long long int n075 = -4294967294;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "-4294967294", n075, "!");
unsigned long long int n076 = -9223372036854775807;
printf ("%-25s %20s %20d %15s\n","unsigned long long int:", "-9223372036854775807", n076, "!");
printf("\nALOKACIJA: %d bitov",sizeof(n076)*8);
return 0;
}
我使用了最新的标准C18进行了编译:
gcc -std=c18 -o 001 001.c
运行二进制文件后,我立即得到以下输出:
***********************************************************************************
IZVORNA KODA BINARNA KODA OPOMBA
signed char: 255 -1 !
signed char: 128 -128 !
signed char: 127 127 MAX - C18
signed char: 1 1 C18
signed char: 0 0 C18
signed char: -1 -1 C18
signed char -127 -127 C18
signed char -128 -128 MIN - C18
ALOKACIJA: 8 bitov
***********************************************************************************
IZVORNA KODA BINARNA KODA OPOMBA
unsigned char: 255 255 MAX - C18
unsigned char: 128 128 C18
unsigned char: 127 127 C18
unsigned char: 1 1 C18
unsigned char: 0 0 MIN - C18
unsigned char: -1 255 !
unsigned char: -127 129 !
unsigned char: -128 128 !
ALOKACIJA: 8 bitov
***********************************************************************************
short int: 65535 -1 !
short int: 32768 -32768 !
short int: 32767 32767 MAX - C18
short int: 32766 32766 C18
short int: 1 1 C18
short int: 0 0 C18
short int: -1 -1 C18
short int: -32767 -32767 C18
short int: -32768 -32768 MIN - C18
ALOKACIJA: 16 bitov
***********************************************************************************
unsigned short int: 65535 65535 MAX - C18
unsigned short int: 32768 32768 C18
unsigned short int: 32767 32767 C18
unsigned short int: 32766 32766 C18
unsigned short int: 1 1 C18
unsigned short int: 0 0 MIN - C18
unsigned short int: -1 65535 !
unsigned short int: -32767 32769 !
unsigned short int: -32768 32768 !
ALOKACIJA: 16 bitov
***********************************************************************************
int: 4294967295 -1 !
int: 2147483648 -2147483648 !
int: 2147483647 2147483647 MAX - C18
int: 1 1 C18
int: 0 0 C18
int: -1 -1 C18
int: -2147483647 -2147483647 C18
int: -2147483648 -2147483648 MIN - C18
ALOKACIJA: 32 bitov
***********************************************************************************
unsigned int: 4294967295 -1 !
unsigned int: 4294967294 -2 !
unsigned int: 2147483648 -2147483648 !
unsigned int: 2147483647 2147483647 MAX - C18
unsigned int: 1 1 C18
unsigned int: 0 0 MIN - C18
unsigned int: -1 -1 ?
unsigned int: -2147483647 -2147483647 ?
unsigned int: -2147483648 -2147483648 ?
ALOKACIJA: 32 bitov
***********************************************************************************
long int: 9223372036854775807 -1 !
long int: 4294967294 -2 !
long int: 2147483648 -2147483648 !
long int: 2147483647 2147483647 MAX - C18
long int: 1 1 C18
long int: 0 0 C18
long int: -1 -1 C18
long int: -2147483647 -2147483647 MIN - C18
long int: -2147483648 -2147483648 ?
long int: -2147483649 2147483647 !
long int: -4294967294 2 !
long int: -9223372036854775807 1 !
ALOKACIJA: 64 bitov
***********************************************************************************
unsigned long int: 9223372036854775807 -1 !
unsigned long int: 4294967294 -2 !
unsigned long int: 2147483648 -2147483648 !
unsigned long int: 2147483647 2147483647 MAX - C18
unsigned long int: 1 1 C18
unsigned long int: 0 0 MIN - C18
unsigned long int: -1 -1 ?
unsigned long int: -2147483648 -2147483648 ?
unsigned long int: -4294967294 2 !
unsigned long int: -9223372036854775807 1 !
ALOKACIJA: 64 bitov
***********************************************************************************
long long int: 9223372036854775807 -1 !
long long int: 4294967294 -2 !
long long int: 2147483648 -2147483648 !
long long int: 2147483647 2147483647 MAX - C18
long long int: 1 1 C18
long long int: 0 0 MIN - C18
long long int: -1 -1 ?
long long int: -2147483648 -2147483648 ?
long long int: -4294967294 2 !
long long int: -9223372036854775807 1 !
ALOKACIJA: 64 bitov
***********************************************************************************
unsigned long long int: 9223372036854775807 -1 !
unsigned long long int: 4294967294 -2 !
unsigned long long int: 2147483648 -2147483648 !
unsigned long long int: 2147483647 2147483647 MAX - C18
unsigned long long int: 1 1 C18
unsigned long long int: 0 0 MIN - C18
unsigned long long int: -1 -1 ?
unsigned long long int: -2147483648 -2147483648 ?
unsigned long long int: -4294967294 2 !
unsigned long long int: -9223372036854775807 1 !
一切正常,直到unsigned int
,long int
,unsigned long int
,long long int
和unsigned long long int
都一样,有的甚至违反了标准(! ),在第20页上明确定义了最大值和最小值:
因此很明显(a)GCC工具链存在问题,并且(b)您应避免使用int
,这是C编程语言的灾难,因此这就是标准C11提出{ {1}}定义了新的整数数据类型:
和每个专业编程人员都在使用新的数据类型,其中保留了旧的数据类型以与较旧的软件兼容,但应避免使用。
也请注意在线课程。如果他们使用旧的数据类型,那么他们显然不了解C,因此您应该避免这些教训。