473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

order of evaluation of increment operators

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!
Nov 14 '05 #1
7 1842
In article <85**************************@posting.google.com >,
ra******@fastmail.fm (Rajorshi) wrote:
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.
Nov 14 '05 #2
> 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.
..
Nov 14 '05 #3
In article <5d**************************@posting.google.com > sa*****@zworg.com (sameer oak.) writes:
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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #4
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 :)
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>... In article <85**************************@posting.google.com >,
ra******@fastmail.fm (Rajorshi) wrote:
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.

Nov 14 '05 #5
[snips]

On Wed, 21 Jul 2004 23:41:44 -0700, Rajorshi wrote:
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.
Nov 14 '05 #6
In <85**************************@posting.google.com > ra******@fastmail.fm (Rajorshi) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> writes:
[snips]
On Wed, 21 Jul 2004 23:41:44 -0700, Rajorshi wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
25
by: haroon | last post by:
Consider this function, void fun (int i, int j) { printf ("i = %d : j = %d", i, j); } I call it like this: fun (n++, n);
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
6
by: marsarden | last post by:
--------pq.c-------- #include <stdio.h> void main() { int a=2,*p,*q; p=&a;q=&a; printf("%d %d\n",*p++,*(q++)); printf("%d\n",a); p=&a;q=&a; printf("%d %d\n",*p,(*q)++);
3
by: andreas ames | last post by:
Hi all, recently I came across a line of code like the following: if seq.erase(seq.begin(), seq.end()) != seq.end() /* ... */ It made me wonder if this is just bogus or if it even can...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.