Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 635 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the '-->' operator in C/C++?

#1
After reading [Hidden Features and Dark Corners of C++/STL][1] 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. I would assume this is also valid C since it works in GCC as well.

Here's the code:

#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}

Output:

9 8 7 6 5 4 3 2 1 0

Where is this defined in the standard, and where has it come from?

[1]:

[To see links please register here]

Reply

#2
The usage of `-->` has historical relevance. Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture. Using `-->` suggests that `x` is going to `0`, and appeals to those with mathematical backgrounds.
Reply

#3
There is a space missing between `--` and `>`. `x` is post decremented, that is, decremented after checking the condition `x>0 ?`.
Reply

#4
`--` is the **decrement** operator and `>` is the **greater-than** operator.

The two operators are applied as a single one like `-->`.
Reply

#5
Utterly geek, but I will be using this:

#define as ;while

int main(int argc, char* argv[])
{
int n = atoi(argv[1]);
do printf("n is %d\n", n) as ( n --> 0);
return 0;
}
Reply

#6
Anyway, we have a "goes to" operator now. `"-->"` is easy to be remembered as a direction, and "while x goes to zero" is meaning-straight.

Furthermore, it is a little more efficient than `"for (x = 10; x > 0; x --)"` on some platforms.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through