简体   繁体   English

关于C中sizeof运算符的困惑

[英]Confusion about sizeof operator in C

I'm confused about sizeof operator in C. 我对C中的sizeof运算符感到困惑。

#include <stdio.h>

int main(void)
{
    char num1=1, num2=2, result;
    result = num1 + num2;
    printf("Size of result: %d \n",sizeof result);
    printf("Size of result: %d \n",sizeof(num1+num2));
}

The results are 1 and 4 respectively. 结果分别为1和4。 Why does this happen? 为什么会这样?

TL;DR answer: TL; DR回答:

  • sizeof result is same as sizeof(char) . sizeof resultsizeof(char)相同。
  • sizeof(num1+ num2) is same as sizeof (int) why? sizeof(num1+ num2)sizeof (int)相同为什么?

In your case, they produce 1 (guaranteed by standard) and 4 (may vary), respectively. 在您的情况下,它们分别产生1(标准保证)和4(可能变化)。

That said , sizeof produces a result of type size_t , so you should %zu format specifier to print the value. 也就是说sizeof产生类型size_t的结果,因此你应该使用%zu格式说明符来打印该值。


Why: 为什么:

First, for the addition operator + , quoting C11 , chapter §6.5.6 首先,对于加法运算符+ ,引用C11 ,章节§6.5.6

If both operands have arithmetic type, the usual arithmetic conversions are performed on them. 如果两个操作数都具有算术类型,则对它们执行通常的算术转换。

Regarding usual arithmetic conversions , §6.3.1.8/p1 关于通常的算术转换 ,§6.3.1.8/ p1

[....] Otherwise, the integer promotions are performed on both operands.[...] [....]否则,将对两个操作数执行整数提升 。[...]

and then from §6.3.1.1,/p2, 然后从§6.3.1.1,/ p2开始,

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int ; 如果int可以表示原始类型的所有值(由宽度限制,对于位字段),则该值将转换为int ; otherwise, it is converted to an unsigned int . 否则,它将转换为unsigned int These are called the integer promotions . 这些被称为整数促销

So, sizeof(num1+num2) is the same as sizeof(int) . 因此, sizeof(num1+num2)sizeof(int)相同。

result is of char type, therefore sizeof is giving 1 while num1+num2 promoted to int type and therefore it gives 4 (size of int ). resultchar类型,因此sizeof1num1+num2提升为int类型,因此它给出4int大小)。

Note that when an arithmetic operation is performed on a type smaller than that of int and all of it's value can be represented by int then result will be promoted to int type. 请注意,当对小于int的类型执行算术运算时,所有值的值都可以用int表示,那么result将被提升为int类型。

num1 + num2 is becoming integer and hence the output is 4 whereas result is char which outputs 1. num1 + num2变为整数,因此输出为4,而result为char,输出1。

You can refer this article Integer Promotion : 你可以参考这篇文章整数推广

If an int can represent all values of the original type, the value is converted to an int; 如果int可以表示原始类型的所有值,则该值将转换为int; otherwise, it is converted to an unsigned int. 否则,它将转换为unsigned int。 These are called the integer promotions. 这些被称为整数促销。 All other types are unchanged by the integer promotions. 整数促销不会更改所有其他类型。

the size of one char is 1byte, a char can hold values up to 127(unsigned up to 255). 一个char的大小是1byte,char可以保存最多127个值(无符号最多255个)。 when you say something like (a + b) a temporary variable is created and used to add a to b, because a and b can hold only 127, they are promoted by the compiler to be an int, just to be sure. 当你说(a + b)之类的东西时,会创建一个临时变量并用于向b添加a,因为a和b只能容纳127,所以编译器将它们提升为int,只是为了确定。

which is logical because if a = 100 and b = 100, the user would like to see 200 when he adds them and not 73 (which is the result of an overflow). 这是合乎逻辑的,因为如果a = 100且b = 100,用户希望在添加它们时看到200而不是73(这是溢出的结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM