我们什么时候应该用sizeof的使用和不使用括号 [英] When should we use sizeof with and without parentheses
问题描述
typedef struct rem{
int addr;
char addrbuf[32];
} foo;
这两个$ C $的CS返回相同的结果。
Both of these codes return the same results
foo addr;
printf("size is: %d\n",sizeof addr);
printf("size is: %d\n",sizeof (foo));
尺寸为:36
尺寸为:36
但是,当我们应该用的sizeof
带和不带括号?
But when should we use sizeof
with and without parentheses?
推荐答案
在使用的sizeof
与类型,的需要绕式括号。当使用的前pression使用它,的你不知道。但是,你当然可以将它们包含在此情况下,你不必担心在这种情况下,运营商precedence。随着运营商少见像这样的,少的人会确保precedence,所以清晰度有一定的帮助。
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.
所以我说这是preferable一直使用它们。
So I'd say it's preferable to use them always.
这篇关于我们什么时候应该用sizeof的使用和不使用括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!