473,698 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1984
mr********@gmai l.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********@gmai l.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********@gmai l.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********@gmai l.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
3571
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 then it prints out the function in postfix notation. I need to add an evaluate function that evaluates the postfix notation obtained by the program. I was hoping that someone could please give me some advice as to how to add the evaluate function...
5
2631
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 to what I want to do but am unable to get it to work. So if someone could please help me it would greatly be appreciated. Thanks for your help. code: #include <cstdio> #include <cstdlib>
3
1701
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 same precedence and think its from right to left...... well, can someone plss enlighten me how this works ?? thanx.
19
11303
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> #include <stdlib.h> #include <string.h> #include <ctype.h>
6
1364
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 variable x in main, I would expect the following to happen: - &x is passed to func_one, so int* p1x is pointing to x (int* p1x = &x) - func_one then dereferences p1x to increment x; at this point x = 1 - func_one passes p1x to func_two, so int* p2x =...
5
2112
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 means *(p++)? I think I am wrong. (so, associativity is not for operand?) But for *++p, right to left associativity means *(++p) is correct. So by definition of associativity, I guess it only applys on grouped
25
1575
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 general rule as to when this type of increment occurs? ( I understand that the increment in "c = ++*s" occurs immediately before assignment, so my guess is the same
8
7831
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
6541
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 static Stack operators = new Stack(); private static Stack operands = new Stack();
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.