NO

Author Topic: priority of operations  (Read 13398 times)

vedro-compota

  • Guest
priority of operations
« on: May 15, 2012, 11:48:11 AM »
hi there!)
guys , please tell me - is there any difference between
Code: [Select]
str++;
next= str;
   
and
Code: [Select]
next= str; relative to next variable?

big thanks in advance)

czerny

  • Guest
Re: priority of operations
« Reply #1 on: May 15, 2012, 01:14:47 PM »
Sure! Explore it in the debugger.

CommonTater

  • Guest
Re: priority of operations
« Reply #2 on: May 15, 2012, 03:18:10 PM »
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...

Code: [Select]
#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

  • Guest
Re: priority of operations
« Reply #3 on: May 15, 2012, 07:54:10 PM »
sorry! i meant
Code: [Select]
str++;
next= str;
   
and
Code: [Select]
next= str++;

CommonTater

  • Guest
Re: priority of operations
« Reply #4 on: May 15, 2012, 08:05:44 PM »
sorry! i meant
Code: [Select]
str++;
next= str;     
   
and
Code: [Select]
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...
Code: [Select]
next = ++str;
then next would = 11, since str would be incremented before the assignment.




vedro-compota

  • Guest
Re: priority of operations
« Reply #5 on: May 28, 2012, 03:06:22 PM »
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: priority of operations
« Reply #6 on: May 28, 2012, 03:35:04 PM »
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

  • Guest
Re: priority of operations
« Reply #7 on: May 28, 2012, 03:37:35 PM »
Quote
before (pre) or after (post) the preceding operation.
understand) thank you, Frankie )

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: priority of operations
« Reply #8 on: May 28, 2012, 03:41:59 PM »
yes ....but in "C programming language"  "++"  has higher priority than "=".
Where does it say that? That doesn't make any sense...
Quote
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
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

Quote
It 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

  • Guest
Re: priority of operations
« Reply #9 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.

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

  • Guest
Re: priority of operations
« Reply #10 on: May 28, 2012, 03:49:33 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.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: priority of operations
« Reply #11 on: May 28, 2012, 03:52:32 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: priority of operations
« Reply #12 on: May 28, 2012, 03:55:36 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

  • Guest
Re: priority of operations
« Reply #13 on: May 28, 2012, 03:57:36 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

  • Guest
Re: priority of operations
« Reply #14 on: May 28, 2012, 04:00:28 PM »
Quote
It 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 )

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