473,403 Members | 2,183 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,403 software developers and data experts.

Question about postfix ++ precedence

Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen

Feb 7 '06 #1
5 1974
mr********@gmail.com wrote:
Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.


Precedence controls the order of application. Postfix has side effect
(changing the value of the operand) which is only observable at the
next sequence point. The result of the postfix increment or decrement
is the value of the operand _before_ it's applied. No contradiction.

V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #2
mr********@gmail.com wrote:
Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.


Because precedence and order-of-evaluation are unrelated.

Feb 7 '06 #3
> Because precedence and order-of-evaluation are unrelated.

Ok... what's the difference, then?

Glen

Feb 7 '06 #4
mr********@gmail.com wrote:
Because precedence and order-of-evaluation are unrelated.


Ok... what's the difference, then?

Glen


If you don't concern yourself with such things like overflow,
precedence determines the logic, while order of execution is
at the liberty of the optimizer.

unsigned a, b, c; // no overflow
...
unsigned d = a + b - c;

'+' and '-' have the same precedence and while they are usually
grouped from left to right, the compiler is free to evaluate the
above expression as if it were written

unsigned d = a + (b - c);

or

unsigned d = (a - c) + b;

In that context we say that the order of execution is unspecified.

That's how I see it anyway. A proper book on on computer science
must explain it better.

V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #5
On 6 Feb 2006 17:21:02 -0800, mr********@gmail.com wrote:
Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen


With an example:

a=b+c++;

Operator precedence tells the compiler to interpret that line as

a= b+ (c++); // YES

and not something like

a=(b+c)++; // NO

But order of evaluation is unspecified. The expression could be
evaluated as:

Either:
-- calculate b+c
-- increment c
-- store calculation in a

Or:

-- calculate b+c
-- store calculation in a
-- increment c

whichever fits better the compiler, depending on lots of things, such
as optimization level, surrounding operations... Same compiler may
compile the expression in many different ways, as long as the
calculation is correct.

Regards,

Zara
Feb 7 '06 #6

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

Similar topics

1
by: Charlie | last post by:
I have the first part of the project done but I'm having difficulty deciding how to add an evaluate function to this program. The program asks the user to enter a function in infix notation and...
5
by: Henry Jordon | last post by:
ok I have my problem entering in an expression in infix notation and it outputs the postfix notation. I now need to evaluate the postfix notation. I have some code written and there are comments as...
3
by: maadhuu | last post by:
hi, i am a bit confused as to how *i++ (i is a pointer) works.....the postfix + has a higher precedence than prefix ++ and i think precedence is from left to right whereas ,prefix ++ and * have...
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
6
by: Simon | last post by:
I'm trying to understand pointers in a little more detail, and have written a test program (reproduced below) to experiment with passing pointers between functions. Thinking only about the...
5
by: fdmfdmfdm | last post by:
Associativity in C takes two forms: left to right and right to left. I think the K&R book lacks something... For example, *p++, since the associativity is from right to left, do this expression...
25
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.