Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average
Thread Closed 


Logic Unknown - help needed filter_list
Author
Message
Logic Unknown - help needed #1
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
Confusedmiling:


Logic Unknown - help needed #2
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
Confusedmiling:


RE: Logic Unknown - help needed #3
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
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]


RE: Logic Unknown - help needed #4
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
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]


RE: Logic Unknown - help needed #5
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?
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: Logic Unknown - help needed #6
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?
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: Logic Unknown - help needed #7
(04-18-2013, 06:50 AM)ArkPhaze Wrote:
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?

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/41763...nce-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.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]


RE: Logic Unknown - help needed #8
(04-18-2013, 06:50 AM)ArkPhaze Wrote:
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?

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/41763...nce-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.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]


RE: Logic Unknown - help needed #9
(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.
You should read about sequence points too. And maybe this question at stackoverflow: http://stackoverflow.com/questions/41763...nce-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.

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 )
[Image: OilyCostlyEwe.gif]


RE: Logic Unknown - help needed #10
(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.
You should read about sequence points too. And maybe this question at stackoverflow: http://stackoverflow.com/questions/41763...nce-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.

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 )
[Image: OilyCostlyEwe.gif]








Users browsing this thread: 1 Guest(s)