Pelles C forum
C language => Beginner questions => Topic started by: vedro-compota on May 15, 2012, 11:48:11 AM
-
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)
-
Sure! Explore it in the debugger.
-
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...
-
sorry! i meant
str++;
next= str;
and
next= str++;
-
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.
-
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
-
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.
-
before (pre) or after (post) the preceding operation.
understand) thank you, Frankie )
-
yes ....but in "C programming language" "++" has higher priority than "=".
Where does it say that? That doesn't make any sense...
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 (http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#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
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.
-
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....
-
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.
-
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
-
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
-
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
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...
-
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 )
test 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 -
increment, or decrement, is done before (pre) or after (post) the preceding operation.
so thanks)
-
Ah ... I understand you completely. "priority" does not relevant to the problem - as it had been said ... - as frankie said =
This is not a problem of priorities
-
[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" ))
More exactly ... it did work, it just didn't do what you expected.
-
one more my mistake (due to carelessness - during post typing).... -
replacing
next= str++
by
str++; next= str; // actually i needed firstly incremented str
-
one more my mistake.... -
replacing
Code: [Select]
str++; next= str;
next= str++
by
str++; next= str; // actually i needed firstly incremented
Then you could have used....
next = ++str;
-
one more my mistake (due to carelessness - during post typing).... -
replacing
next= str++
by
str++; next= str; // actually i needed firstly incremented str
yes - next = ++str; is alternative)
I've understand this from your first answer in this thread))