Stackoverflow热门问题(八)-为什么sizeof(x++)不会让x自增?
stackoverflow热门问题目录如有翻译问题欢迎评论指出,谢谢。
为什么sizeof(x++)不会让x自增?
- Neigyl R. Noval asked:
- 我在dev c++ windows中写下了如下代码:
#includeint main() {int x = 5;printf("%d and ", sizeof(x++)); // note 1printf("%d\n", x); // note 2return 0;}- 我觉得注释一的代码执行后
x应该变成6,但输出是这样的: 4 and 5- 有人能解释一下为什么注释1的代码执行后x不会自增吗?
- Answers:
- pmg - vote: 552
- 见C99标准:
- 6.5.3.4/2
sizeof运算符产生其操作数的的大小(以字节为单位),这个操作数可能是表达式或类型名。其大小取决于操作数的类型。其结果是整型。如果以变长数组类型作为操作数,那么会运算这个操作数;否则将不会被运算,且结果是整型常量。- 译者注:
- 下面第三个回答比它晚了两分钟,内容相似且还对其作了解释,可惜这vote太低了。
- 看不懂的可以看下面第三个回答。
- 见C99标准:
- codaddict - vote: 192
- sizeof是一个编译时运算符,所以在编译时,
sizeof与其操作数会被结果值代替。操作数不会被运算(除非是变长数组);sizeof只注重其操作数结果的类型。- 译者注:感觉是因为C不像python,做个加法运算后类型就变掉,所以直接不运算了。
short func(short x) { // this function never gets called !!printf("%d", x); // this print never happensreturn x;}int main() {printf("%d", sizeof(func(3))); // all that matters to sizeof is the// return type of the function. return 0;}- 输出:
2- 当我机器上的
short占2字节时,是上面的输出结果。 - 将函数返回类型改成
double: double func(short x) {// rest all same- 输出将为8。
- 译者注:我在自己电脑试了
sizeof(a = a + 2);不会对a值产生影响。
- sizeof是一个编译时运算符,所以在编译时,
- pmg - vote: 552
Why does sizeof(x++) not increment x?
- Neigyl R. Noval asked:
- Here is the code compiled in dev c++ windows:
- 我在dev c++ windows中写下了如下代码:
#includeint main() {int x = 5;printf("%d and ", sizeof(x++)); // note 1printf("%d\n", x); // note 2return 0;}- I expect
xto be 6 after executing note 1. However, the output is:- 我觉得注释一的代码执行后
x应该变成6,但输出是这样的:
- 我觉得注释一的代码执行后
4 and 5- Can anyone explain why
xdoes not increment after note 1?- 有人能解释一下为什么注释1的代码执行后x不会自增吗?
- Here is the code compiled in dev c++ windows:
- Answers:
- pmg - vote: 552
- From the C99 Standard (the emphasis is mine)
- 见C99标准:
- 6.5.3.4/2
- 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. The result is an integer. 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.
sizeof运算符产生其操作数的的大小(以字节为单位),这个操作数可能是表达式或类型名。其大小取决于操作数的类型。其结果是整型。如果以变长数组类型作为操作数,那么会运算这个操作数;否则将不会被运算,且结果是整型常量。- 译者注:
- 下面第三个回答比它晚了两分钟,内容相似且还对其作了解释,可惜这vote太低了。
- 看不懂的可以看下面第三个回答。
- From the C99 Standard (the emphasis is mine)
- codaddict - vote: 192
sizeofis a compile-time operator, so at the time of compilationsizeofand its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters.- sizeof是一个编译时运算符,所以在编译时,
sizeof与其操作数会被结果值代替。操作数不会被运算(除非是变长数组);sizeof只注重其操作数结果的类型。- 译者注:感觉是因为C不像python,做个加法运算后类型就变掉,所以直接不运算了。
- sizeof是一个编译时运算符,所以在编译时,
short func(short x) { // this function never gets called !!printf("%d", x); // this print never happensreturn x;}int main() {printf("%d", sizeof(func(3))); // all that matters to sizeof is the// return type of the function. return 0;}- Output:
- 输出:
2- as
shortoccupies 2 bytes on my machine.- 当我机器上的
short占2字节时,是上面的输出结果。
- 当我机器上的
- Changing the return type of the function to
double:- 将函数返回类型改成
double:
- 将函数返回类型改成
double func(short x) {// rest all same- will give
8as output.- 输出将为8。
- 译者注:
- 我在自己电脑试了
sizeof(a = a + 2);不会对a值产生影响。
- 我在自己电脑试了
- sarnold - vote: 49
sizeof(foo)tries really hard to discover the size of an expression at compile time:sizeof(foo)在编译时难以发现表达式的大小:
- 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. The result is an integer. 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.
sizeof运算符产生其操作数的的大小(以字节为单位),这个操作数可能是表达式或类型名。其大小取决于操作数的类型。其结果是整型。如果以变长数组类型作为操作数,那么会运算这个操作数;否则将不会被运算,且结果是整型常量。
- In short: variable length arrays, run at runtime. (Note: Variable Length Arrays are a specific feature -- not arrays allocated with
malloc(3).) Otherwise, only the type of the expression is computed, and that at compile time.- 总的来说:变长数组在运行时运行(注:变长数组是特性,不是用malloc分配的数组)。否则,只有表达式的类型被计算,在编译时被计算。
- 译者注:感觉表达的应该是对于sizeof(),如果操作数是变长数组,就在运行时执行,否则在编译时执行。
- 总的来说:变长数组在运行时运行(注:变长数组是特性,不是用malloc分配的数组)。否则,只有表达式的类型被计算,在编译时被计算。
- pmg - vote: 552


共有 0 条评论