问题描述
阅读 C ++/STL的隐藏功能和死角在 comp.lang.c ++.moderated
上,我完全惊讶以下代码段在Visual Studio 2008和G ++ 4.4中均已编译并正常工作.
After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated
, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4.
代码如下:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
输出:
9 8 7 6 5 4 3 2 1 0
我认为这是C,因为它也可以在GCC中使用.该标准在哪里定义,它是从哪里来的?
I'd assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from?
推荐答案
->
不是运算符.实际上,它是两个单独的运算符,分别是-
和>
.
-->
is not an operator. It is in fact two separate operators, --
and >
.
条件代码的代码递减 x
,同时返回 x
的原始(未递减)值,然后将原始值与 0
进行比较使用>
运算符.
The conditional's code decrements x
, while returning x
's original (not decremented) value, and then compares the original value with 0
using the >
operator.
为更好地理解,该语句可以写为:
while( (x--) > 0 )
这篇关于什么是“-"C/C ++中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!