C语言中的整数变量占用2字节还是4字节?它取决于哪些因素?
大多数教科书都说整型变量占用2个字节。 但是当我运行一个程序打印一个整数数组的连续地址时,它显示了4的差值。
C语言中的整数变量占用2字节还是4字节?它取决于哪些因素?
大多数教科书都说整型变量占用2个字节。 但是当我运行一个程序打印一个整数数组的连续地址时,它显示了4的差值。
当前回答
c99n1256标准草案
http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
int和所有其他整数类型的大小都是实现定义的,C99只指定:
最小尺寸保证 类型之间的相对大小
5.2.4.2.1“整数类型的大小<limits.h>”给出了最小大小:
1[…它们的实施定义值应等于或大于所示值(绝对值)[…] Uchar_max 255 // 2 8−1 .使用实例 Ushrt_max 65535 // 2 16−1 .使用实例 Uint_max 65535 // 2 16−1 .使用实例 Ulong_max 4294967295 // 2 32−1 .使用实例 Ullong_max 18446744073709551615 // 2 64−1
6.2.5“类型”指:
8对于任意两个具有相同符号和不同整数转换秩的整数类型 (见6.3.1.1),整数转换秩较小的类型的值范围为a 另一类型值的子范围。
6.3.1.1“布尔、字符和整数”确定相对转换等级:
1 Every integer type has an integer conversion rank defined as follows: The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char. The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any. For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3
其他回答
我知道它等于sizeof(int)int的大小实际上取决于编译器。在处理器是16位的时候,一个int是2字节。现在,在32位和64位系统上,它通常是4个字节。
不过,使用sizeof(int)是获得执行程序的特定系统的整数大小的最佳方法。
编辑:修正了在大多数64位系统上int为8字节的错误语句。例如,它在64位GCC上是4字节。
#include <stdio.h>
int main(void) {
printf("size of int: %d", (int)sizeof(int));
return 0;
}
这个返回4,但它可能依赖于机器。
C“int”的大小是2字节还是4字节? C语言中的整数变量占用2字节还是4字节?
C语言允许“字节”不是每“字节”8位。
CHAR_BIT非位域(字节)的最小对象的位数
大于8的值越来越少见。为了获得最大的可移植性,使用CHAR_BIT而不是8。在C语言中,以比特为单位的int的大小是sizeof(int) * CHAR_BIT。
#include <limits.h>
printf("(int) Bit size %zu\n", sizeof(int) * CHAR_BIT);
它取决于哪些因素?
int位大小通常为32或16位。C指定的最小范围:
int INT_MIN -32767类型对象的最小值 int INT_MAX +32767类型对象的最大值 C11dr§5.2.4.2.1
int的最小范围强制比特大小至少为16 -即使处理器是“8位”。在专门的处理器中可以看到64位这样的大小。其他的数值,如18、24、36等,在历史平台上出现过,或者至少在理论上是可能的。现代编码很少担心非2的整数次幂的比特大小。
计算机的处理器和体系结构驱动int位大小的选择。
然而,即使使用64位处理器,出于兼容性原因,编译器的int大小可能是32位的,因为大型代码库依赖于int是32位的(或32/16)。
这是回答这个问题的一个很好的来源。
但这个问题是一种永远真实的回答:“是的。”两个。”
这取决于您的体系结构。如果您要在16位或更少的机器上工作,它不能是4字节(=32位)。如果您在32位或更好的机器上工作,则其长度为32位。
为了弄清楚,让你的程序准备输出一些可读的东西,并使用“sizeof”函数。返回您声明的数据类型的字节大小。但是要小心使用数组。
如果你声明int t[12];它将返回12*4字节。要得到这个数组的长度,只需使用sizeof(t)/sizeof(t[0])。 如果你要建立一个函数,它会计算发送数组的大小,记住If
typedef int array[12];
int function(array t){
int size_of_t = sizeof(t)/sizeof(t[0]);
return size_of_t;
}
void main(){
array t = {1,1,1}; //remember: t= [1,1,1,0,...,0]
int a = function(t); //remember: sending t is just a pointer and equal to int* t
print(a); // output will be 1, since t will be interpreted as an int itselve.
}
So this won't even return something different. If you define an array and try to get the length afterwards, use sizeof. If you send an array to a function, remember the send value is just a pointer on the first element. But in case one, you always knows, what size your array has. Case two can be figured out by defining two functions and miss some performance. Define function(array t) and define function2(array t, int size_of_t). Call "function(t)" measure the length by some copy-work and send the result to function2, where you can do whatever you want on variable array-sizes.
这主要取决于你使用的平台,取决于不同的编译器。现在在大多数编译器中,int是4字节。 如果你想检查你的编译器正在使用什么,你可以使用sizeof(int)。
main()
{
printf("%d",sizeof(int));
printf("%d",sizeof(short));
printf("%d",sizeof(long));
}
c编译器唯一承诺的是short的大小必须等于或小于int, long的大小必须等于或大于int。因此,如果int的size是4,那么short的size可能是2或4,但不会大于它。long和int也是如此。上面还说长短不能一样。