简体   繁体   English

sizeof运算符的操作数

[英]operands for sizeof operator

I understood result of 我明白了结果

int nData = 10;
printf("%d", sizeof(nData + 2.0));

is "8" 是“8”

why each result of 为什么每个结果

int nData = 10;
printf("%d", sizeof(nData = 2.0));
printf("%d", sizeof(nData += 2.0));

is not 8 but 4? 不是8而是4? Why nData cannot be 12.0 or 12 by sizeof(nData += 2.0) ? 为什么nData不能是sizeof(nData += 2.0) 12.012

Because 2.0 is a constant of type double , the expression nData + 2.0 has type double as per the "usual arithmetic conversions" specified in section 6.3.1.8 of the C standard: 因为2.0是double类型的常量,所以表达式nData + 2.0具有类型double ,根据C标准的6.3.1.8节中指定的“通常算术转换”:

First, if the corresponding real type of either operand is long double , the other operand is converted, without change of type domain, to a type whose corresponding real type is long double. 首先,如果任一操作数的相应实数类型是long double,则另一个操作数在不改变类型域的情况下被转换为对应的实类型为long double的类型。

Otherwise, if the corresponding real type of either operand is double , the other operand is converted, without change of type domain, to a type whose corresponding real type is double 否则,如果任一操作数的相应实数类型为double,则将另一个操作数转换为类型为domain的类型,其类型为对应的实数类型为double

So sizeof evaluates to the size of a double . 因此sizeof评估为double的大小。

In the case of nData = 2.0 and nData += 2.0 , each expression has type int because that is the type of the left-hand side of the assignment. nData = 2.0nData += 2.0的情况下,每个表达式都具有int类型,因为这是赋值左侧的类型。 So sizeof evaluated to the size of an int . 所以sizeof评估为int的大小。

Also, the operand of the sizeof operator is evaluated for its type only at compile time . 此外,仅在编译时评估sizeof运算符的操作数的类型。 This means that any assignment or increment is not evaluated at runtime. 这意味着不会在运行时评估任何赋值或增量。 So in your second example, nData will still have the value 10 after the two lines that use sizeof . 所以在你的第二个例子中, nData在使用sizeof的两行之后仍然具有值10。 The only time the operand of sizeof is evaluated at run time is if the operand is a variable length array. 在运行时评估sizeof的操作数的唯一时间是操作数是否是可变长度数组。

Why nData cannot be 12.0 or 12 by sizeof(nData += 2.0) ? 为什么nData不能是sizeof(nData += 2.0) 12.012

Well, sizeof is a compiler time operator , and it operates on the data type, not the value. 那么, sizeof是一个编译器时间操作符 ,它对数据类型而不是值进行操作。

In other words, except when the argument is VLA , the operand of the sizeof is not evaluated. 换句话说,除非参数是VLA ,否则不会计算sizeof的操作数。

Quoting C11 , chapter §6.5.3.4 引用C11 ,章节§6.5.3.4

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. sizeof运算符产生其操作数的大小(以字节为单位),该操作数可以是表达式或类型的带括号的名称。 The size is determined from the type of the operand. 大小由操作数的类型确定。 If the type of the operand is a variable length array type, the operand is evaluated; 如果操作数的类型是可变长度数组类型,则计算操作数; otherwise, the operand is not evaluated and the result is an integer constant. 否则,不评估操作数,结果是整数常量。

So, in your case, 所以,在你的情况下,

  printf("%d", sizeof(nData + 2.0));   // data == int, 2.0 == double

is the same as 是相同的

 printf("%d", sizeof(double));  //  as the resulting "type" of the addition, 
                                //   'int + double' would be  'double'

which should better be 哪个应该更好

 printf("%zu", sizeof(double));   //not %d

as sizeof yields size_t type. sizeof产生size_t类型。

Also, regarding the 2.0 being of type double , from chapter §6.4.4.2 另外,关于2.0double类型,来自章节§6.4.4.2

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. 浮点常量具有有效位部分,后面可以跟一个指数部分和一个指定其类型的后缀。 The components of the significand part may include a digit sequence representing the whole-number part, followed by a period (.), followed by a digit sequence representing the fraction part. 有效数部分的分量可以包括表示整数部分的数字序列,后面是句点(。),后面是表示分数部分的数字序列。 [...] [...]

and

An unsuffixed floating constant has type double . 未填充的浮点常量具有double类型。 [...] [...]

Thus, a constant value like 2.0 has the type double . 因此,像2.0这样的常量值具有double类型。

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

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