priority of operations

Started by vedro-compota, May 15, 2012, 11:48:11 AM

Previous topic - Next topic

vedro-compota

hi there!)
guys , please tell me - is there any difference between
str++;
next= str;
   
and
next= str;
relative to next variable?

big thanks in advance)

czerny

Sure! Explore it in the debugger.

CommonTater

Yep there's a difference str++;  amounts to str = str + 1;  assuming str is a numeric variable rather than an array, string or struct.

you can look at it in the debugger (as suggested) or you can write some test code to see the results of various operations...


#include <stdio.h>

int main (void)
{
   int x = 10;

   printf(" x = %d\n",x);

   x++;

   printf(" x = %d\n",x);

   // and so on

  return 0;
}


Small "test proggies" are a good way to see what a given function or combination of functions will do...

vedro-compota

sorry! i meant
str++;
next= str;
   
and
next= str++;

CommonTater

Quote from: vedro-compota on May 15, 2012, 07:54:10 PM
sorry! i meant
str++;
next= str;     
   
and
next= str++;   

Ok ... if str = 10...  In the first one next will = 11, in the second one it will = 10.  This is because str is not incremented until after the assignment to next.

If you did...

next = ++str;

then next would = 11, since str would be incremented before the assignment.




vedro-compota

yes ....but in "C programming language"  "++"  has higher priority than "=".
do you now some good table with operations priority ? in Dennis Ritchi book there's only this one  - http://fkn.ktu10.com/?q=node/1371

frankie

This is not a problem of priorities.
Study better your K&R
The '++' or '--' have two forms: preincrement, or predecrement, and postincrement, or postdecrement, depending if they precede or follow the variable.
The meaning is that the increment, or decrement, is done before (pre) or after (post) the preceding operation.

So 'next=++str' will first increment 'str' then assign it to 'next'.
'next=str++' will assing str to 'next' first than str will be incremented.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

vedro-compota

Quotebefore (pre) or after (post) the preceding operation.
understand) thank you, Frankie )

Bitbeisser

Quote from: vedro-compota on May 28, 2012, 03:06:22 PM
yes ....but in "C programming language"  "++"  has higher priority than "=".
Where does it say that? That doesn't make any sense...
Quotedo you now some good table with operations priority ? in Dennis Ritchi book there's only this one  - http://fkn.ktu10.com/?q=node/1371
Well, that list that you just mentioned (in self-reference) to shows just the opposite of what you mentioned (even without being able to read Russian). It shows the ++/-- operator as second in priority (right after parenthesis) while the assignment is at the very bottom...

Actually I just found a comprehensive operator priority list on Wikipedia, which likewise shows the opposite of what you just stated: Operator Precedence

Don't have a K&R reference handy for posting, but an online reference of VC++ (not that this matters in particular, the very same rules apply to all C compilers) clearly states

QuoteIt is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression.

CommonTater

For starts K&R C is not Pelles C... look in the help file for Pelle's own operator precidence lists.  K&R was written before C-99 and way before C-11 ... things change, my friend. Programming books are not bibles, they merely provide you with a *starting point* by relating basic concepts and syntax... K&R, for example, tells you about 5% of what you will need to know to write decent programs... the rest comes from experience, documentation of actual compilers and, yes, a LOT of trial and error.

Second just because one operator has precidence over another does NOT mean the compiler is going to break order of execution to accomodate it.  Order of precedence (not "priority") only means that when all sections of an expression are equally parenthetic, the highest value operators will be parsed first...

Do as I suggested and write small test programs to actually SEE the results... If they differ from your expectation, that is almost certainly an indication there's something you're not understanding.  You will never make a compiler do things your way... it's your job to learn how to do things it's way....

CommonTater

Quote from: Bitbeisser on May 28, 2012, 03:41:59 PM
Actually I just found a comprehensive operator priority list on Wikipedia,

Actually Ralf... since this can be "implementation defined" the biblical reference is Pelle's own help file, describing the exact behaviour of his compiler...

Help -> Contents -> C Language Reference -> Espressions and Assignments -> Operator precidence.

Bitbeisser

Quote from: CommonTater on May 28, 2012, 03:44:27 PM
For starts K&R C is not Pelles C... look in the help file for Pelle's own operator precidence lists.  K&R was written before C-99 and way before C-11 ... things change, my friend. Programming books are not bibles, they merely provide you with a *starting point* by relating basic concepts and syntax... K&R, for example, tells you about 5% of what you will need to know to write decent programs... the rest comes from experience, documentation of actual compilers and, yes, a LOT of trial and error.
Sorry, but the order of precedence doesn't have anything to do with K&R versus ANSI C. Any change in this would fundamentally break the C language (or C++, C#, Perl, etc, any language that uses this kind of notation).
It's just one of those peculiarities of C that someone needs to properly understand and I am not sure if our friend here keeps hitting a language barrier...

Ralf

Bitbeisser

Quote from: CommonTater on May 28, 2012, 03:49:33 PM
Quote from: Bitbeisser on May 28, 2012, 03:41:59 PM
Actually I just found a comprehensive operator priority list on Wikipedia,

Actually Ralf... since this can be "implementation defined" the biblical reference is Pelle's own help file, describing the exact behaviour of his compiler...

Help -> Contents -> C Language Reference -> Espressions and Assignments -> Operator precidence.
See my previous post and the Wikipedia reference that I posted. I am sure that even in K&R is a list with the same order, I just don't have a copy handy to post a reference.

Again, this is a fundamental "feature" of C and can not be implementation dependent. That would be disastrous....

Ralf

CommonTater

Quote from: Bitbeisser on May 28, 2012, 03:52:32 PM
Sorry, but the order of precedence doesn't have anything to do with K&R versus ANSI C. Any change in this would fundamentally break the C language (or C++, C#, Perl, etc, any language that uses this kind of notation).

No argument, my friend... C would be very broken if these basic concepts were changed or ignored.  However, since there are often eccentricities and compiler specific decisions made I've always made it my policy to trust the documentation for the actual compiler over the more generalized information found on the web...  I dunno... when I pull something like this off Wikipedia I alway feel like I'm trying to fix my Ford while referring to a Chevy manual :D

Quote
It's just one of those peculiarities of C that someone needs to properly understand and I am not sure if our friend here keeps hitting a language barrier...

Could well be... although he appears to understand us well enough...

vedro-compota

QuoteIt shows the ++/-- operator as second in priority (right after parenthesis) while the assignment is at the very bottom...
yes - i've said this - this's the reason why i started the thread.
where  is the problem? second position is higher than bottom, isn't it? sorry for Russian )

Quotetest programs to actually SEE the results
CommonTater , I  got this problem from practise had quickly solved it by replacing 
str++; next= str; by next= str++
and only after that I asked why
next= str++ "doesn't work" ))
I also get a good answer -
Quoteincrement, or decrement, is done before (pre) or after (post) the preceding operation.
so thanks)