-1

I was trying and wondering some on c code. I saw some relevant code in here. I could not reach the person who gave the answer somehow. So I wanted to ask you :

    int x = 100;

      while (0 <-------------------- x) {

          printf("%d ", x);

    }

    return 0;

I answered this question, yes, there is an error. https://stackoverflow.com/a/27672749/17556719

But as you can see, it answered like this : 90 80 70 60 50 40 30 20 10.

< is an operator in c.
<- is not an operator in c.
- is an operator in c.
-- is an operator in c.
--- is not operator in c.

As far as I know maximal munch says ok guy you take -- as an operator and go on.

Samples can be multiplied. (with - and + operators) From now on, how can I understand/parse this code? And is it just me getting an error?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • I think you're saying that this code gives you an error when you compile it. If so, can you copy the exact code you're compiling and the specific error message that you get? – Paul Hankin Feb 17 '22 at 10:35
  • You need `#ifdef __cplusplus` / `#error bad compiler` / `#endif` – pmg Feb 17 '22 at 10:36
  • I think the answer to your problem is that this code is valid C++, but not valid C. – Paul Hankin Feb 17 '22 at 10:38
  • Check operator pre-decrement to see what is happening there with `x`: https://en.cppreference.com/w/cpp/language/operator_incdec – rturrado Feb 17 '22 at 10:57

1 Answers1

5

You are compiling in C++, not C.

The -- operator can only act on an object. In C, the -- operator produces only a value, not an lvalue. (An lvalue is an expression that may designate an object.) So the compiler will report that 0 <-------------------- x has an error.

In C++, the -- operator produces an lvalue. So the compiler will allow 0 <-------------------- x.

In both C and C++, the parsing is straightforward:

  • In <--------------------, the longest sequence of characters (starting from the beginning) that constitutes a token is <, so it is taken as a token and removed, leaving --------------------.
  • In --------------------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ------------------.
  • In ------------------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ----------------.
  • In ----------------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving --------------.
  • In --------------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ------------.
  • In ------------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ----------.
  • In ----------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving --------.
  • In --------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ------.
  • In ------, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving ----.
  • In ----, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, leaving --.
  • In --, the longest sequence of characters (starting from the beginning) that constitutes a token is --, so it is taken as a token and removed, after which the compiler goes on to further source text.

In both C and C++, the sequence of tokens is 0 < -- -- -- -- -- -- -- -- -- -- x. In C, this expression violates constraints. In C++, it does not.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • If your question is how the compiler parses the minuses to operators you have the answer in your linked question: https://stackoverflow.com/a/2604267/3684343 – mch Feb 17 '22 at 10:47