int c = 0的结果; cout<< c ++<< c; [英] The result of int c=0; cout<<c++<<c;

查看:112
本文介绍了int c = 0的结果; cout<< c ++<< c;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为应该是01,但有人说它的未定义,任何原因?

I think it should be 01 but someone says its "undefined", any reason for that?

推荐答案

c ++ 既是增量,又是赋值。当分配发生时(在该行上的其他代码之前或之后)由编译器决定。它可以在 cout 之后或之前发生。

c++ is both an increment and an assignment. When the assignment occurs (before or after other code on that line) is left up to the discretion of the compiler. It can occur after the cout << or before.

这可以在C99标准
http://www.open-std.org/JTC1/SC22 /wg14/www/docs/n1124.pdf

您可以在第28页的pdf或第5.1.2.3节中找到

You can find it on page 28 in the pdf or section 5.1.2.3


p的实际增量可以发生在前一个序列点和下一个序列点之间的任何时间

the actual increment of p can occur at any time between the previous sequence point and the next sequence point

由于有人要求C ++标准(因为这是一个C ++问题),可以在1.9.15节第10页(或24的pdf格式)中找到

Since someone asked for the C++ standard (as this is a C++ question) it can be found in section 1.9.15 page 10 (or 24 in pdf format)


单个运算符的操作数和单个表达式的子表达式的求值不受影响

evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced

它还包括以下代码块:

i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // i becomes 9
i = i++ + 1; // the behavior is undefined

我觉得C99标准的解释更清楚,两种语言。

I feel that the C99 standard's explanation is clearer, but it is true in both languages.

这篇关于int c = 0的结果; cout&lt;&lt; c ++&lt;&lt; c;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆