-9

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
  • 35
  • 3
  • 18
Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36

3 Answers3

15

--> 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.
  • 122,712
  • 22
  • 185
  • 265
  • Re "*`<-` is simply not an operator at all.*", But it can be part of valid code. In the same spirit of `<--`, there's `x <- 4` (`x < -4`). – ikegami Jun 20 '21 at 07:21
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
  • 11,269
  • 13
  • 67
  • 105
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
  • 1,054
  • 1
  • 11
  • 22