-10

I was going through K&R C book and got through this --> operator in the precedence table. So, I wondered if there was a similar operator i.e., <-- and wrote the following program:

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

It worked perfectly fine. So why isn't <-- is not considered as an operator? (as it is not in the precedence table!) and what is it's precedence?

Pkarls
  • 37
  • 3
  • 18
Pruthvi Raj
  • 2,956
  • 2
  • 21
  • 36

3 Answers3

14

--> is not one operator, it is two; (post) decrement and less than. C is whitespace agnostic for the most part, so:

x --> y
/* is the same as */
x-- > y

<-- is the same idea:

x <-- y
/* is the same as */
x < --y

Perhaps you are confusing -> with -->. -> dereferences a pointer to get at a member of the type it refers to.

typedef struct
{
    int x;
} foo;

int main(void)
{
    foo f = {1};
    foo *fp = &f;
    printf("%d", fp->x);
    return 0;
}

<- is simply not an operator at all.

Ed S.
  • 119,398
  • 20
  • 176
  • 254
3

This is not an operator but two operators: < and --. The code is identical to

#include<stdio.h>
void main()
{
   int x = 5;
   while(0 < --x)
       printf("%d",x);
}
Charles
  • 10,775
  • 13
  • 64
  • 98
2

As I see it, that is just a "less than" < operator followed by decreasing the variable x (--). It is not one operator, but two. And -- has precedence over <.

cnluzon
  • 997
  • 1
  • 10
  • 20