sizeof 运算符的操作数
operands for sizeof operator
我理解
的结果
int nData = 10;
printf("%d", sizeof(nData + 2.0));
是“8”
为什么每个结果
int nData = 10;
printf("%d", sizeof(nData = 2.0));
printf("%d", sizeof(nData += 2.0));
不是8而是4?为什么 nData
不能由 sizeof(nData += 2.0)
变成 12.0
或 12
?
Why nData
cannot be 12.0
or 12
by sizeof(nData += 2.0)
?
嗯,sizeof
是一个 compiler time operator,它对数据类型而不是值进行操作。
换句话说,除了参数is VLA,sizeof
的操作数不计算。
引用 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. 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.
所以,在你的情况下,
printf("%d", sizeof(nData + 2.0)); // data == int, 2.0 == double
与
相同
printf("%d", sizeof(double)); // as the resulting "type" of the addition,
// 'int + double' would be 'double'
哪个更好
printf("%zu", sizeof(double)); //not %d
as sizeof
产生 size_t
类型。
此外,关于 2.0
类型 double
,来自章节 §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. [...]
和
An unsuffixed floating constant has type double
. [...]
因此,像 2.0
这样的常量值具有 double
.
类型
因为 2.0 是类型 double
的常量,表达式 nData + 2.0
的类型 double
按照 C 标准第 6.3.1.8 节中指定的 "usual arithmetic conversions" :
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.
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
因此 sizeof
计算出 double
的大小。
在 nData = 2.0
和 nData += 2.0
的情况下,每个表达式的类型都是 int
,因为这是赋值左侧的类型。所以 sizeof
评估为 int
.
的大小
此外,sizeof
运算符的操作数仅在 编译时 评估其类型。这意味着任何赋值或增量都不会在 运行 时计算。因此,在您的第二个示例中,nData
在使用 sizeof
的两行之后仍将具有值 10。 sizeof
的操作数唯一一次在 运行 时计算是操作数是否为可变长度数组。
我理解
的结果int nData = 10;
printf("%d", sizeof(nData + 2.0));
是“8”
为什么每个结果
int nData = 10;
printf("%d", sizeof(nData = 2.0));
printf("%d", sizeof(nData += 2.0));
不是8而是4?为什么 nData
不能由 sizeof(nData += 2.0)
变成 12.0
或 12
?
Why
nData
cannot be12.0
or12
bysizeof(nData += 2.0)
?
嗯,sizeof
是一个 compiler time operator,它对数据类型而不是值进行操作。
换句话说,除了参数is VLA,sizeof
的操作数不计算。
引用 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. 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.
所以,在你的情况下,
printf("%d", sizeof(nData + 2.0)); // data == int, 2.0 == double
与
相同 printf("%d", sizeof(double)); // as the resulting "type" of the addition,
// 'int + double' would be 'double'
哪个更好
printf("%zu", sizeof(double)); //not %d
as sizeof
产生 size_t
类型。
此外,关于 2.0
类型 double
,来自章节 §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. [...]
和
An unsuffixed floating constant has type
double
. [...]
因此,像 2.0
这样的常量值具有 double
.
因为 2.0 是类型 double
的常量,表达式 nData + 2.0
的类型 double
按照 C 标准第 6.3.1.8 节中指定的 "usual arithmetic conversions" :
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.
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
因此 sizeof
计算出 double
的大小。
在 nData = 2.0
和 nData += 2.0
的情况下,每个表达式的类型都是 int
,因为这是赋值左侧的类型。所以 sizeof
评估为 int
.
此外,sizeof
运算符的操作数仅在 编译时 评估其类型。这意味着任何赋值或增量都不会在 运行 时计算。因此,在您的第二个示例中,nData
在使用 sizeof
的两行之后仍将具有值 10。 sizeof
的操作数唯一一次在 运行 时计算是操作数是否为可变长度数组。