“- ->” 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

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.