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

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 10 May, 2020 · 1 min read

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.

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read