什么是“--"?C/C++ 中的运算符? [英] What is the "-->" operator in C/C++?
问题描述
阅读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++ 中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!