“- ->” operator in C and C++

By | May 10, 2020

Actually, “- – >” is not an operator, but this is combination of two separate operators, “- -” and “>” .

So, if you write it as

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

It will print following output,

9 8 7 6 5 4 3 2 1 0

Because conditional’s code decrements x,

while (x- - > 0)

Statement could be written as follows:

while( (x- -) > 0 ) 

x- – (post decrement) is equivalent to x = x-1.

So, code is same as:

while(x > 0) {
    x = x-1;
    // logic
}

// post decrement done when x <= 0
x- -;   

Same as,

while (x- -)
{
   printf("%d ", x);
} 

For non-negative numbers.

Hence, these are two different operators: - - and > described respectively in §5.2.6/2 and §5.9 of the C++03 Standard. Decrement operation is faster than incrementing on the x86 architecture.


Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.