![]() |
|
Logic Unknown - help needed - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: Logic Unknown - help needed (/Thread-Logic-Unknown-help-needed) |
Logic Unknown - help needed - Creed_mybb_import11544 - 04-17-2013 i have run these codes Code: #include<stdio.h>
#include<conio.h>
int main(){
int x,y=3;
x=(y++)+(++y);
printf("%d",x);
getch();
}and got the output as Quote:8 i thought 1stly,++y is evaluated then y increments to 4 and then y++ is evaluated but y does not increments that time .. so x will show 7... but output i am getting is 8 .. i am using Dev c++ compiler .. could anyone please tell me what is the actual logic for output = 8 ?? and is there any possibility to get output = 7 if different compiler is used ? thanks a lot in advance miling:
Logic Unknown - help needed - Creed_mybb_import11544 - 04-17-2013 i have run these codes Code: #include<stdio.h>
#include<conio.h>
int main(){
int x,y=3;
x=(y++)+(++y);
printf("%d",x);
getch();
}and got the output as Quote:8 i thought 1stly,++y is evaluated then y increments to 4 and then y++ is evaluated but y does not increments that time .. so x will show 7... but output i am getting is 8 .. i am using Dev c++ compiler .. could anyone please tell me what is the actual logic for output = 8 ?? and is there any possibility to get output = 7 if different compiler is used ? thanks a lot in advance miling:
RE: Logic Unknown - help needed - Deque - 04-17-2013 According to the C standard the behaviour is undefined in this case. It might be that the compiler evaluates the line from left to right and another compiler evaluates the line from right to left. So yes, it depends on the compiler which output you get. Edit: Read more here: https://en.wikipedia.org/wiki/Sequence_point RE: Logic Unknown - help needed - Deque - 04-17-2013 According to the C standard the behaviour is undefined in this case. It might be that the compiler evaluates the line from left to right and another compiler evaluates the line from right to left. So yes, it depends on the compiler which output you get. Edit: Read more here: https://en.wikipedia.org/wiki/Sequence_point RE: Logic Unknown - help needed - ArkPhaze - 04-18-2013 Code: int x,y=3;
x=(y++)+(++y);
printf("%d",x);This is really just basic stuff... First, y starts off with 3 (it's the important one to pay attention to in this code), but first it's important to know the difference between ++? and ?++. When we do y++ and add ++y this is what happens: 1. We take y's current value as the first number 2. increment y by 1 3. Take that last value of y which was 3 before it got incremented to 4, and add it to the incremented value of y, which is 4 incremented to 5 immediately Here's what that would look like: Code: x=3+5;You could get 7 by doing this if you were getting 8... Code: x=(y++)+(y++);Why in the world are you using DevC++ though? RE: Logic Unknown - help needed - ArkPhaze - 04-18-2013 Code: int x,y=3;
x=(y++)+(++y);
printf("%d",x);This is really just basic stuff... First, y starts off with 3 (it's the important one to pay attention to in this code), but first it's important to know the difference between ++? and ?++. When we do y++ and add ++y this is what happens: 1. We take y's current value as the first number 2. increment y by 1 3. Take that last value of y which was 3 before it got incremented to 4, and add it to the incremented value of y, which is 4 incremented to 5 immediately Here's what that would look like: Code: x=3+5;You could get 7 by doing this if you were getting 8... Code: x=(y++)+(y++);Why in the world are you using DevC++ though? RE: Logic Unknown - help needed - Deque - 04-18-2013 (04-18-2013, 06:50 AM)ArkPhaze Wrote: You are wrong, ArkPhaze. The behaviour is not defined by the C language specification. Every compiler might return a different result here, because some evaluate ++y before y++ and some do it the other way around. You should read about sequence points too. And maybe this question at stackoverflow: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points Quote:At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (§1.9/7) This is violated, if you do: x=(y++)+(++y); Quote:Why in the world are you using DevC++ though? Let's hope Creed is using Orwells Dev-C++ and not the unmaintained original from Bloodshed. RE: Logic Unknown - help needed - Deque - 04-18-2013 (04-18-2013, 06:50 AM)ArkPhaze Wrote: You are wrong, ArkPhaze. The behaviour is not defined by the C language specification. Every compiler might return a different result here, because some evaluate ++y before y++ and some do it the other way around. You should read about sequence points too. And maybe this question at stackoverflow: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points Quote:At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (§1.9/7) This is violated, if you do: x=(y++)+(++y); Quote:Why in the world are you using DevC++ though? Let's hope Creed is using Orwells Dev-C++ and not the unmaintained original from Bloodshed. RE: Logic Unknown - help needed - Psycho_Coder - 04-18-2013 (04-18-2013, 07:56 AM)Deque Wrote: You are wrong, ArkPhaze. The behaviour is not defined by the C language specification. Every compiler might return a different result here, because some evaluate ++y before y++ and some do it the other way around. Yes you're right , compiler's have there own preference of operation that is from left to right or right to left , the traditional turbo C or C++ compiler uses right to left but the dev c++ ( by bloodshed ) uses left to right . and Creed is using Dev C++ , the one by BloodShed , (I know him as I referred him on HC ) RE: Logic Unknown - help needed - Psycho_Coder - 04-18-2013 (04-18-2013, 07:56 AM)Deque Wrote: You are wrong, ArkPhaze. The behaviour is not defined by the C language specification. Every compiler might return a different result here, because some evaluate ++y before y++ and some do it the other way around. Yes you're right , compiler's have there own preference of operation that is from left to right or right to left , the traditional turbo C or C++ compiler uses right to left but the dev c++ ( by bloodshed ) uses left to right . and Creed is using Dev C++ , the one by BloodShed , (I know him as I referred him on HC ) |