什么时候我们应该使用带括号和不带括号的sizeof [重复]

时间:2022-09-11 11:40:43

This question already has an answer here:

这个问题在这里已有答案:

typedef struct rem{
    int  addr;
    char addrbuf[32];
} foo;

Both of these codes return the same results

这两个代码都返回相同的结果

foo addr;
printf("size is: %d\n",sizeof addr);
printf("size is: %d\n",sizeof (foo));

size is: 36

大小是:36

size is: 36

大小是:36

But when should we use sizeof with and without parentheses?

但是什么时候我们应该使用带括号和不带括号的sizeof?

2 个解决方案

#1


17  

When using sizeof with a type, you need parentheses around the type. When using it with an expression, you don't. But you can of course include them in this case as well, and you don't have to worry about operator precedence in such case. With uncommon operators such as this one, fewer people would be sure of the precedence, so clarity certainly helps.

将sizeof与类型一起使用时,需要围绕类型使用括号。将它与表达式一起使用时,则不会。但是你也可以在这种情况下包含它们,在这种情况下你不必担心运算符优先级。对于像这样的不常见的运营商,更少的人会确定优先级,因此清晰度肯定有帮助。

So I'd say it's preferable to use them always.

所以我会说最好总是使用它们。

#2


10  

[expr.sizeof]/1:

The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id。

Thus the parentheses are required for types only. If you prefer to use parentheses for clarity and consistency (as I do) you can always use them though, as parentheses around an expression form another expression.
The operator precedence of sizeof is not very well known and could cause irritations.

因此,括号仅适用于类型。如果您更喜欢使用括号来表示清晰度和一致性(就像我一样),您可以随时使用它们,因为表达式周围的括号形成另一个表达式。 sizeof的运算符优先级不是很清楚,可能会引起烦恼。

Also, for the sizeof... operator, you always have to use parentheses (another reason for consistency).

此外,对于sizeof ...运算符,您始终必须使用括号(一致性的另一个原因)。

#1


17  

When using sizeof with a type, you need parentheses around the type. When using it with an expression, you don't. But you can of course include them in this case as well, and you don't have to worry about operator precedence in such case. With uncommon operators such as this one, fewer people would be sure of the precedence, so clarity certainly helps.

将sizeof与类型一起使用时,需要围绕类型使用括号。将它与表达式一起使用时,则不会。但是你也可以在这种情况下包含它们,在这种情况下你不必担心运算符优先级。对于像这样的不常见的运营商,更少的人会确定优先级,因此清晰度肯定有帮助。

So I'd say it's preferable to use them always.

所以我会说最好总是使用它们。

#2


10  

[expr.sizeof]/1:

The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id。

Thus the parentheses are required for types only. If you prefer to use parentheses for clarity and consistency (as I do) you can always use them though, as parentheses around an expression form another expression.
The operator precedence of sizeof is not very well known and could cause irritations.

因此,括号仅适用于类型。如果您更喜欢使用括号来表示清晰度和一致性(就像我一样),您可以随时使用它们,因为表达式周围的括号形成另一个表达式。 sizeof的运算符优先级不是很清楚,可能会引起烦恼。

Also, for the sizeof... operator, you always have to use parentheses (another reason for consistency).

此外,对于sizeof ...运算符,您始终必须使用括号(一致性的另一个原因)。