order of evaluation of increment operators

R

Rajorshi

Hi,
I am relatively new to c programming. I have a question regarding C's
increment (++) operator. KnR says that unary operators like ++ associate
from right to left. Having that information, I still dont understand in
which order the following statements are evaluated :

x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide
whether
x+++y is x + (++y) or (x++) + y .

I am obviously missing something...

Thanks in advance!
 
C

Christian Bau

Hi,
I am relatively new to c programming. I have a question regarding C's
increment (++) operator. KnR says that unary operators like ++ associate
from right to left. Having that information, I still dont understand in
which order the following statements are evaluated :

x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide
whether
x+++y is x + (++y) or (x++) + y .

If you don't know what a statement does, then don't use it.

There is no need to ever write code like "x+++y". If you find it used
anywhere, instead of trying to figure out what it does find the
programmer who wrote it, get a printed copy of the C++ Standard (not the
C Standard, because the C++ Standard is larger and heavier) and hit that
programmer with it until he or she changes the code.

Apart from that, the compiler builds tokens from left to right, taking
as many characters as possible. So if the compiler sees a '+' character,
followed by another '+' character, the result is never two '+' tokens
but always a single '++' token.
 
S

sameer oak.

Having that information, I still dont understand in
which order the following statements are evaluated :
x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide whether
x+++y is x + (++y) or (x++) + y .

I am obviously missing something...
....
the reason is operator precedence and the way they are associated with
the
operands.
as you have mentioned in the above example, x+++y, the ++ operator has
higher
precedence over + operator.
if you'd done something like this, x+(++y), the parenthesis were
executed first.

in the 2nd case, x++++y, the compiler tries to attach ++ operator to
both the
operands but cannot succeed to interpret the operation and thus issues
an error
for ``y'', something of this sort:
---
foo.c: In function `main':
foo.c:6: error: invalid lvalue in increment
foo.c:6: error: parse error before "y"
---

the last case is really confusing, x+++++y. am sure the error here is
again "invalid lvalue." someone from the compilers can post a more
appropriate answer for the last case.
..
 
D

Dik T. Winter

> the last case is really confusing, x+++++y. am sure the error here is
> again "invalid lvalue." someone from the compilers can post a more
> appropriate answer for the last case.

The compiler parses it as: x ++ ++ + y.
 
R

Rajorshi

Hi,
Hey thanks a lot ! But before you flame me further - I would like to
tell you
that I have NO intention of writing such code. Its just that these
type of
questions are actually being asked at exams and interviews of large
software companies (no, not FSF).
programmer who wrote it, get a printed copy of the C++ Standard (not the
C Standard, because the C++ Standard is larger and heavier) and hit that
programmer with it until he or she changes the code.

:)), good one

So you mean that when the compiler sees something like
x+++y, it considers it as x++ + y and then evaluates it.
Hmm... makes sense... (silly me...)
Thanks a lot, problem seems to have been solved (for now that is :)
 
D

Dan Pop

In said:
Hi,
I am relatively new to c programming. I have a question regarding C's
increment (++) operator. KnR says that unary operators like ++ associate
from right to left. Having that information, I still dont understand in
which order the following statements are evaluated :

x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide
whether
x+++y is x + (++y) or (x++) + y .

I am obviously missing something...

Yup, you've missed the requirement to read the FAQ *before* posting.

Dan
 
K

Kelsey Bjarnason

[snips]

x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide
whether
x+++y is x + (++y) or (x++) + y .

I am obviously missing something...

Probably what you're missing is what I refer to as the "maximal munch
rule": when confronted with a mess such as the above, the compiler creates
tokens by taking as much as possible at each step. Thus, given "x+++y",
it will create the following tokens: x, ++, +, y. If it treated the
"+++" as + followed by ++, it wouldn't be applying the maximal munch rule
properly; if it treated it as +++, it would be broken, as the code, while
sloppy, is valid, but the production of a +++ wouldn't be.
 
K

Keith Thompson

Kelsey Bjarnason said:
[snips]
x+++y
x++++y
x+++++y and so on.
I mean how does the compiler decide
whether
x+++y is x + (++y) or (x++) + y .

I am obviously missing something...

Probably what you're missing is what I refer to as the "maximal munch
rule": when confronted with a mess such as the above, the compiler creates
tokens by taking as much as possible at each step. Thus, given "x+++y",
it will create the following tokens: x, ++, +, y. If it treated the
"+++" as + followed by ++, it wouldn't be applying the maximal munch rule
properly; if it treated it as +++, it would be broken, as the code, while
sloppy, is valid, but the production of a +++ wouldn't be.

And the important thing to remember is that the maximal munch rule
applies even if a valid parse would otherwise be possible. For
example:
x+++++y
is tokenized as
x ++ ++ + y
(which causes a syntax error) even though
x ++ + ++ y
would be legal. Tokenization and parsing are separate phases, and
earlier phases operate without reference to what might happen in later
ones.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top