freelanceprogrammers.org Forum Index » C
Re: operators precedence
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
From: rohit sidhar [mailto:sidhar_rohit@...]
>my que is
>
>main()
>{ int x=3,z;
>
>z= ++x - x++;
>printf("%d%d",x,z);
>}
>
>here the output is 5 and 0......why so?...
Because what you have there is undefined in the C or C++ languages. Any
output is `correct` including what you think or don`t think. You are
modifying x twice between sequence points. See
<http://www.eskimo.com/~scs/C-faq/q3.2.html>
>i my opinion the
>output should be 5 and 2 as first x++ will be evaluated to
>take x=3 and then x will be incremented after that ++x will be
>evaluated as the associvity of operator ++ is from right to left.......
Just don`t do it.
--
PJH
The simple man`s approach to exception handling is to put a try-catch
around the whole application and just restart the application on error.
That`s what Microsoft does anyway.
- Thomas Hruska sums up MS in C-Prog
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
From: Sreeram B S [mailto:sreeramabsc@...]
>Hello Rohit,
> The answer which is obtained ie 5 and 0 is correct and convincing.
It is not correct. Not if you`re programming in C or C++ anyway.
>Explaination: Always preincrement happens, executes the
>current statement and then postincrement happens.
> With above lines, first x=3. Now ++x is executed and x is
>now 4. Then z is evaluated as x-x and so z=0. Now the
>postincrement (ie x++) is evaluated and so x=x+1 ie x=4+1 and so x=5.
> Hence upon doing: printf("%d %d
",x,z); we get 5 and 0.
>
>Thanks,
>Sreeram
>
>
>rohit sidhar <sidhar_rohit@...> wrote:
>
> my que is
>
> main()
> { int x=3,z;
>
> z= ++x - x++;
> printf("%d%d",x,z);
> }
>
> here the output is 5 and 0......why so?...i my opinion
>the output should be 5 and 2 as first x++ will be evaluated to
>take x=3 and then x will be incremented after that ++x will be
>evaluated as the associvity of operator ++ is from right to left.......
--
PJH
"The difference between genius and stupidity is that genius had its
limits."
- Albert Einstein
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
From: manik singhal [mailto:maniksinghal@...]
>--- rohit sidhar <sidhar_rohit@...> wrote:
>
>> my que is
>>
>> main()
>> { int x=3,z;
>>
>> z= ++x - x++;
>> printf("%d%d",x,z);
>> }
>>
>> here the output is 5 and 0......why so?...i my
>> opinion the output should be 5 and 2 as first x++
>> will be evaluated to take x=3 and then x will be
>> incremented after that ++x will be evaluated as the
>> associvity of operator ++ is from right to
>> left.......
>>
>hi,
>First ++x gets evaluated (being pre fix),
But C and C++ are allowed to evaluate the second half of the expression
(i.e. x++) first.
>makes x 4.
>then expression gets evaluated: => 4 -4 = 0;
>Then x++ gets evaluated (being post fix), increments x
>to 5
>Regards
>Manik
--
PJH
"During my service in the United States Congress, I took the initiative
in creating the Internet."
- Al Gore
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 12 Jan 2005
Posts: 1
Re: operators precedence
Hello Shyan,
I might not have learnt about `sequence points`, `undefined behaviour` etc..in my school, but the answer/explaination what I have given is convincing. You can check it on you own. You cannot say that it is misleading/ wrong if you dont know about it. And moreover your reply says that my explaination is wrong but you have not even bothered to reply as to what is wrong and what is the correct interpretation.
This is a place where people share their ideas and learn things. I have seen such mails from you to others also in the group. Kindly dont send such mails. If you feel some thing is wrong in a mail, say what is correct instead of simply saying that it is wrong or misleading.
Hope this helps...
Thanks,
Sreeram
Shyan Lam <sflam@...> wrote:
Reply embedded...> From: Sreeram B S [mailto:sreeramabsc@...] > Sent: Tuesday, January 11, 2005 5:59 AM> To: Programmers-Town@yahoogroups.com> Subject: Re: (PT) operators precedence>> Hello Rohit,> The answer which is obtained ie 5 and 0 is correct and convincing. Any output/results are considered "correct" as far as undefined behavior isconcern. > Explaination: Always preincrement happens, executes the current statement> and then postincrement happens.>> With above lines, first x=3. Now ++x is executed and x is now 4. Then z > is evaluated as x-x and so z=0. Now the postincrement (ie x++) is > evaluated and so x=x+1 ie x=4+1 and so x=5.>> Hence upon doing: printf("%d %d
",x,z); we get 5 and 0.Your explanations are
misleading and wrong. You have to learn what are "sequence points", "side effects", and "undefinedbehavior". Until you know all these, you can then claim you know C.If you did not learn these concepts in school, ask for refund.ShyanTo unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more.
Joined: 12 Jan 2005
Posts: 1
Re: operators precedence
Rohit wrote: main() { int x=3,z; z= ++x - x++; printf("%d%d",x,z); }
The output is 5,0.
Explanation:
The operator (-) has its associativity from left to right. So ++x will be evaluated first. So x will now have the value 4 and when the compiler executes x++
it uses the value of x (i.e 4 now) and then only increment it. So Z will be 4 - 4 = 0. As a result of x++
x`s value will be incremented to 5.
....Madhusudhan.S
Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less.
Joined: 12 Jan 2005
Posts: 3
Re: operators precedence
This problem is not associated with precedence or assosiativity. There is a notion in programming languages called "Sequence Points". In C there is a sequence point after ";" operator, and there is a rule in C that "Between consecutive "sequence points" an object`s value can be modified only once by an expression". Now in the expression z= ++x - x++; value of x is modified and used between consequtive sequence points. ++x will increament x and thus value of x is modified but whether x++ will use this modified value or not is undefined. Hence the result is either z=0 or z=1 depending on the compiler. Moral of the story is "Results of such expressions are compiler dependent in C".
With Regards,Ruchin KansalHCL Technologies Ltd.A-11 Sector-16 NOIDA, UP.Phone: 0120-2510701/702/813. Extension: 3130
-----Original Message-----From: Sudhan_RECT [mailto:sudhan_rect@...]Sent: Wednesday, January 12, 2005 12:00 PMTo: Programmers-Town@yahoogroups.comSubject: RE: (PT) operators precedence
Rohit wrote: main() { int x=3,z; z= ++x - x++; printf("%d%d",x,z); }
The output is 5,0.
Explanation:
The operator (-) has its associativity from left to right. So ++x will be evaluated first. So x will now have the value 4 and when the compiler executes x++
it uses the value of x (i.e 4 now) and then only increment it. So Z will be 4 - 4 = 0. As a result of x++
x`s value will be incremented to 5.
....Madhusudhan.S
Do you Yahoo!?Yahoo! Mail - 250MB free storage. Do more. Manage less. To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Joined: 12 Jan 2005
Posts: 1
Re: operators precedence
Hello Shyan,
I have the same opinion as Sreeram. Please answer the questions if u know and make it a good learning centre. If all of us know everything, then no need to have discussions...
Hope we understand this and thanks for ur answers.
- HT2BPSreeram B S <sreeramabsc@...> wrote:
Hello Shyan,
I might not have learnt about `sequence points`, `undefined behaviour` etc..in my school, but the answer/explaination what I have given is convincing. You can check it on you own. You cannot say that it is misleading/ wrong if you dont know about it. And moreover your reply says that my explaination is wrong but you have not even bothered to reply as to what is wrong and what is the correct interpretation.
This is a place where people share their ideas and learn things. I have seen such mails from you to others also in the group. Kindly dont send such mails. If you feel some thing is wrong in a mail, say what is correct instead of simply saying that it is wrong or misleading.
Hope this helps...
Thanks,
Sreeram
Shyan Lam <sflam@...> wrote:
Reply embedded...> From: Sreeram B S [mailto:sreeramabsc@...] > Sent: Tuesday, January 11, 2005 5:59 AM> To: Programmers-Town@yahoogroups.com> Subject: Re: (PT) operators precedence>> Hello Rohit,> The answer which is obtained ie 5 and 0 is correct and convincing. Any output/results are considered "correct" as far as undefined behavior isconcern. > Explaination: Always preincrement happens, executes the current statement> and then postincrement happens.>> With above lines, first x=3. Now ++x is executed and x is now 4. Then z > is evaluated as x-x and so z=0. Now the postincrement (ie x++) is > evaluated and so x=x+1 ie x=4+1 and so x=5.>> Hence upon doing: printf("%d %d
",x,z); we get 5 and 0.Your explanations are
misleading and wrong. You have to learn what are "sequence points", "side effects", and "undefinedbehavior". Until you know all these, you can then claim you know C.If you did not learn these concepts in school, ask for refund.ShyanTo unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!?Yahoo! Mail - Easier than ever with enhanced search. Learn more. To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Yahoo! India Matrimony: Find your life partner
online.
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
From: Sreeram B S [mailto:sreeramabsc@...]
Hello Shyan,
I might not have learnt about `sequence points`, `undefined behaviour` etc..in my school,
Then your course is lacking some fundimental concepts. I`d ask for my money back if it were me.
but the answer/explaination what I have given is convincing.
But misleading, and terribly, terribly wrong when talking about C or C++.
You can check it on you own.
Ok - I`ll bite. Using your empirical evidence, tell me exactly why this program, compiled two different ways, produces different output:
You cannot say that it is misleading/ wrong if you dont know about it. And moreover your reply says that my explaination is wrong but you have not even bothered to reply as to what is wrong and what is the correct interpretation.
Possibly because it is assumed that people programming in C know fundimental things like sequence points. As I said, your course seems lacking.
This is a place where people share their ideas and learn things. I have seen such mails from you to others also in the group. Kindly dont send such mails. If you feel some thing is wrong in a mail, say what is correct instead of simply saying that it is wrong or misleading.
Hope this helps...
Thanks,
Sreeram
Shyan Lam <sflam@...> wrote:
Reply embedded...> From: Sreeram B S [mailto:sreeramabsc@...] > Sent: Tuesday, January 11, 2005 5:59 AM> To: Programmers-Town@yahoogroups.com> Subject: Re: (PT) operators precedence>> Hello Rohit,> The answer which is obtained ie 5 and 0 is correct and convincing. Any output/results are considered "correct" as far as undefined behavior isconcern. > Explaination: Always preincrement happens, executes the current statement> and then postincrement happens.>> With above lines, first x=3. Now ++x is executed and x is now 4. Then z > is evaluated as x-x and so z=0. Now the postincrement (ie x++) is > evaluated and so x=x+1 ie x=4+1 and so x=5.>> Hence upon doing: printf("%d %d
",x,z); we get 5 and 0.Your explanations are misleading and wrong. You have to learn what are "sequence points", "side effects", and "undefinedbehavior". Until you know all these, you can then claim you know C.If you did not learn these concepts in school, ask for refund.ShyanTo unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!?Yahoo! Mail - Easier than ever with enhanced search. Learn more. To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UKTel: +44(0)1454 294556 Fax: +44 (0)1454 299272Website : www.alderley.com Sales : sales@... Service : service@...
This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify it@...
This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
Apologies for the previous mail - sent it too early before including the code I meant to reference...
From: Sreeram B S [mailto:sreeramabsc@...]
Hello Shyan,
I might not have learnt about `sequence points`, `undefined behaviour` etc..in my school,
Then your course is lacking some fundimental concepts. I`d ask for my money back if it were me.
but the answer/explaination what I have given is convincing.
But misleading, and terribly, terribly wrong when talking about C or C++.
You can check it on you own.
Ok - I`ll bit
From: Sreeram B S [mailto:sreeramabsc@...]
Hello Shyan,
I might not have learnt about `sequence points`, `undefined behaviour` etc..in my school,
Then your course is lacking some fundimental concepts. I`d ask for my money back if it were me.
but the answer/explaination what I have given is convincing.
But misleading, and terribly, terribly wrong when talking about C or C++.
You can check it on you own.
Ok - I`ll bite. Using your empirical evidence, tell me exactly why this program, compiled two different ways, produces different output:
$ type c.c#include <stdio.h>
int one(void){ return printf("1 ");}int two(void){ return printf("2 ");}int three(void){ return printf("3 ");}
int main(void){ printf("(1+2)*3
"); printf("
%d", (one() + two()) * three());
printf("
1*(2+3)
"); printf("
%d", one() * (two() + three()));}
$ cl c.c /EHs /Zc:forScope /Wall /W4 /WL /wd4820 /wd4619 /wd4217 /wd4710 /wd4668 /nologo /o c.exec.c
$ c(1+2)*31 2 38
1*(2+3)1 2 38$ cl c.c /EHs /Zc:forScope /Wall /W4 /WL /wd4820 /wd4619 /wd4217 /wd4710 /wd4668 /nologo /o c.exe /O1c.c
$ c(1+2)*32 1 38
1*(2+3)3 2 18$
You cannot say that it is misleading/ wrong if you dont know about it. And moreover your reply says that my explaination is wrong but you have not even bothered to reply as to what is wrong and what is the correct interpretation.
Possibly because it is assumed that people programming in C know fundimental things like sequence points. As I said, your course seems lacking. See <http://www.eskimo.com/~scs/C-faq/q3.2.html> for a brief introduction as to why your current thinking is not entirely right.
OK - I`ll bite. I have checked this. Proof follows.
You are basing your assumptions on the fact that "C must evaluate expressions in a predefined order".
Can you tell me exactly why this program, compiled two different ways, produces two different outputs? (Neither output is wrong by the way.)
$ type c.c#include <stdio.h>
int one(void){ return printf("1 ");}int two(void){ return printf("2 ");}int three(void){ return printf("3 ");}
int main(void){ printf("(1+2)*3
"); printf("
%d", (one() + two()) * three());
printf("
1*(2+3)
"); printf("
%d", one() * (two() + three()));}
$ cl c.c /EHs /Zc:forScope /Wall /W4 /WL /wd4820 /wd4619 /wd4217 /wd4710 /wd4668 /nologo /o c.exec.c
$ c(1+2)*31 2 38
1*(2+3)1 2 38$ cl c.c /EHs /Zc:forScope /Wall /W4 /WL /wd4820 /wd4619 /wd4217 /wd4710 /wd4668 /nologo /o c.exe /O1c.c
$ c(1+2)*32 1 38
1*(2+3)3 2 18$
You cannot say that it is misleading/ wrong if you dont know about it. And moreover your reply says that my explaination is wrong but you have not even bothered to reply as to what is wrong and what is the correct interpretation.
Possibly because it is assumed that people programming in C know fundimental things like sequence points. As I said, your course seems lacking. See <http://www.eskimo.com/~scs/C-faq/q3.2.html> for a brief introduction as to why your current thinking is not entirely right.
--PJH"Real programmers can write assembly code in any language." - Larry Wall
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UKTel: +44(0)1454 294556 Fax: +44 (0)1454 299272Website : www.alderley.com Sales : sales@... Service : service@...
This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify it@...
This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.
Joined: 29 Dec 2004
Posts: 31
Re: operators precedence
From: Sudhan_RECT [mailto:sudhan_rect@...]
> Rohit wrote:
> main()
> { int x=3,z;
>
> z= ++x - x++;
> printf("%d%d",x,z);
> }
>
>The output is 5,0.
It could would be equally valid for the output to be 5,2.
Ignoring the fact that x is being modified twice between sequence points
(see <http://www.eskimo.com/~scs/C-faq/q3.2.html>), it would be equally
valid for this program to print nothing since there is no
at the end
of the string being printed.
[wrong explanation snipped]
--
PJH
It makes it difficult to follow the thread of conversation.
What`s wrong with top posting?
Top posting.
What`s the most annoying thing on email lists?
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 25 Jul 2003
Posts: 48
Re: operators precedence
Reply embedded...
> From: Sreeram B S [mailto:sreeramabsc@...]
> Sent: Tuesday, January 11, 2005 11:15 PM
> To: Programmers-Town@yahoogroups.com
> Subject: RE: (PT) operators precedence
>
> Hello Shyan,
> I might not have learnt about `sequence points`, `undefined behaviour`
> etc..in my school,
"Sequence points" is the basic concept that defines whether a C/C++
statement is well- or ill-formed.
I would suggest you read every section here:
http://www.eskimo.com/~scs/C-faq/s3.html
and here...
http://www.eskimo.com/~scs/readings/undef.981105.html
and Paul`s reply to operator precedence:
http://groups.yahoo.com/group/Programmers-Town/message/4978
> but the answer/explaination what I have given is
> convincing.
It is not. Given the expression:
++x - x++
1. The complier can evaluate either the left operand or the right operand
first. Precedence only comes in *after* the operands are evaluated.
2. pre- and post-operation has "side effects". For ++, the side effect is:
the value is incremented by one. Do you know when is the side effect
settled down? The standard only guarantees the side effects are settled
down at the next sequence point. Before sequence point, the value of `x` is
undetermined.
> You can check it on you own. You cannot say that it is
> misleading/ wrong if you dont know about it.
The standard says very clearly. Between sequence points, a variable can be
modified at most once. Otherwise the result is undefined.
Also modifying and referencing a variable between sequence points also
produce undefined behavior.
Your so call analysis shows only one thing:
Your compiler`s reaction to undefined behavior, given the current compiler
settings. different compiler, different version of the same compiler, or
different setting of the same compiler can produce different result.
What`s the point of such analysis, and to whom you`re going to convince.
> And moreover your reply says
> that my explaination is wrong but you have not even bothered to reply as
> to what is wrong and what is the correct interpretation.
This kind of questions has been popping up at least once every week. Anyone
bother to check the archives can receive tons of information (including the
correct explanation and misleading one).
> This is a place where people share their ideas and learn things. I have
> seen such mails from you to others also in the group. Kindly dont send
> such mails. If you feel some thing is wrong in a mail, say what is correct
> instead of simply saying that it is wrong or misleading.
I did tell you to learn "sequence point", "side effect" and "undefined
behavior", didn`t I?
Shyan
Joined: 06 Jan 2005
Posts: 2
Re: operators precedence
Quoting sidhar_rohit@...:
> my que is
main()
{ intx=3,z;
Now x has value 3
z= ++x/* here x has value 4*/ -x++/* herex has value 4 */;
/* here x has value 5 /
printf("%d%d",x,z);
so,nowoutput is 5 and 0
}
here the output is 5 and 0......whyso?...i my opinion the output should be 5 and 2 as first x++ will beevaluated to take x=3 and then x will be incremented after that ++x willbe evaluated as the associvity of operator ++ is from right toleft.......
---------------------------------
Do you Yahoo!?
Meet the all-new My Yahoo! – Try it today!
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







