really really simple program giving different outputs on 2 compilers&#

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1326 Views - Last Post: 09 September 2011 - 04:31 PM Rate Topic: -----

#1 theju112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-September 11

really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 08:48 AM

i got a program that swaps two variables in a single statement.

but im getting different outputs in turbo c and pelles c :hammer:

the code in pelles c is:

#include<stdio.h>
int main()
{
	int a=10,b=20;
	a=(a+B)/>-(b=a);
	printf("%d%d",a,B)/>;
}



output: 10 10

code in tc is
#include<stdio.h>
#include<conio.h>
int main()
{
 int a=10,b=20;
 clrscr();
 a=(a+B)/>-(b=a);
 printf("%d %d",a,B)/>;
 getch();
}



output 20 10


please dont :gun_bandana: me if there is some silly mistake. im a rookie.

This post has been edited by theju112: 09 September 2011 - 08:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: really really simple program giving different outputs on 2 compilers&#

#2 theju112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 08:54 AM

what is this admin,im not able to change 'B' i have typed in the above code to 'b'.
:2guns:
if anybody out there reading above code,read 'B' as 'b'.the editors not saving changes
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 08:55 AM

Um, you have an = instead of whatever: a=( a + b ) -( b = a );

This post has been edited by baavgai: 09 September 2011 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 08:57 AM

What are the different outputs?
Was This Post Helpful? 0
  • +
  • -

#5 theju112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 08:58 AM

View Postbaavgai, on 09 September 2011 - 08:55 AM, said:

Um, you have an = instead of whatever: a=( a + b ) -( b = a );


what???? where??????

somebody call a doctor im goin blind.

View PostAKMafia001, on 09 September 2011 - 08:57 AM, said:

What are the different outputs?

in tc the outputs correct : 20 10
in pelles c outputs wrong :10 10
Was This Post Helpful? 0
  • +
  • -

#6 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 09:07 AM

This is only possible if the associativity of parenthesis () is right to left.

e.g.
a = ( a + b ) - ( b = a ); // Evaluating the parenthesis at right first
/* ( b = a ) .:. b = 10
( a + b ) .:. ( 20 ), Because b is 10.
a = ( 20 ) - ( 10 )
a = 10
*/



Hope I maked some sense!
Was This Post Helpful? 0
  • +
  • -

#7 theju112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 09:14 AM

View PostAKMafia001, on 09 September 2011 - 09:07 AM, said:

This is only possible if the associativity of parenthesis () is right to left.

e.g.
a = ( a + b ) - ( b = a ); // Evaluating the parenthesis at right first
/* ( b = a ) .:. b = 10
( a + b ) .:. ( 20 ), Because b is 10.
a = ( 20 ) - ( 10 )
a = 10
*/



Hope I maked some sense!

sorry brother,im still :death:
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2146
  • View blog
  • Posts: 3,296
  • Joined: 21-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 09:18 AM

When you read and write a variable without a sequence point in between, the behavior is undefined. Specifically [( a+b )-(b=a) is undefined because there's no sequence point between a+b and b=a. A good compiler will warn you about this.

Edit:

Quote

This is only possible if the associativity of parenthesis () is right to left.


I think what you meant to say here that the operands of the minus operator are evaluated right to left.

Parentheses aren't binary operators so they don't have an associativity nor does associativity imply an order of evaluation (an operator can be right-associative and still evaluates its left operand first).

This post has been edited by sepp2k: 09 September 2011 - 09:32 AM

Was This Post Helpful? 2
  • +
  • -

#9 theju112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 09:59 AM

View Postsepp2k, on 09 September 2011 - 09:18 AM, said:

When you read and write a variable without a sequence point in between, the behavior is undefined. Specifically [( a+b )-(b=a) is undefined because there's no sequence point between a+b and b=a. A good compiler will warn you about this.

Edit:

Quote

This is only possible if the associativity of parenthesis () is right to left.


I think what you meant to say here that the operands of the minus operator are evaluated right to left.

Parentheses aren't binary operators so they don't have an associativity nor does associativity imply an order of evaluation (an operator can be right-associative and still evaluates its left operand first).


ok sir,
i know that a variable cannot be modified more than once between two seq. points.

but here i am modifying a and b exactly once between the sequence point after clrscr() and the seq. point at the end of the statement : a=(a+B)-(b=a).

can you please explain in detail. please make it simple. :sigh:
Was This Post Helpful? 0
  • +
  • -

#10 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 10:36 AM

@sepp2k

Quote

Parentheses aren't binary operators so they don't have an associativity nor does associativity imply an order of evaluation (an operator can be right-associative and still evaluates its left operand first).


I didn't said that they are binary operators. But what else you say -- i don't agree. Because,

That's what Deitel says.

Attached image(s)

  • Attached Image

This post has been edited by AKMafia001: 09 September 2011 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1739
  • View blog
  • Posts: 3,347
  • Joined: 30-May 10

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 10:55 AM

> 5 a=(a+B)-(b=a);
But what you have absolutely no control over is when (a+B) is evaluated compared to when (b=a) is evaluated.

> but here i am modifying a and b exactly once between the sequence point
True, but you're also reading b as well, which is where it all comes unstuck.

Here is my post showing all the fun ways in which assuming an evaluation order (and other crimes) can really mess up your day.

@AKMafia001
But none of that stops (b=a) from happening first.
If (a+b) happens first, and is stored in a temp location, then you win.
if (b=a) happens first, then you lose.

() has no effect on this (see my link).
Even something like ((a)+(b))*(d), the apparent 'depth' of parentheses does NOT stop the compiler from evaluating c first and storing the result in a temp somewhere. () only guides how the results are combined in the end, not how the initial results are calculated.

I think Deitel is wrong, because
http://c-faq.com/expr/precvsooe.html
and

Quote

// c99, 6.5 Expressions
2 Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be accessed only to determine the value to be stored.60)

3 The grouping of operators and operands is indicated by the syntax.61) Except as specified
later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation
of subexpressions and the order in which side effects take place are both unspecified.

a and b are both changed between sequence points, so all bets are off.
Was This Post Helpful? 1
  • +
  • -

#12 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 10:59 AM

Ur expression is first "stacked" before it gets solved... right? So the a=(a+B)-(b=a); gets pushed into the call stack and the computer registers do the solving,etc. (this is a bit deep into the computer's processor so i'm not going there). But, the simple thing is that ur compiler follows a CALLING CONVENTION. Turbo C uses Cdecl, gcc (OR) microsoft uses fastcall and so on.. i hope u get this idea.. anyway here's another program to demo u this..

#include<stdio.h>
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}


Here, the arguements of the function(printf) are passed into stack.. but in ur program the expression elements are passed into the stack. I hope that explains. For more google for "calling conventions ext:pdf"
Was This Post Helpful? -9
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2146
  • View blog
  • Posts: 3,296
  • Joined: 21-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 10:59 AM

@theju112:

Quote

can you please explain in detail. please make it simple


As I said there is no sequence point between ( a+b ) and (b=a), so it is not defined whether b=a is evaluated before or after a+b. If it is evaluated before than the b in a+b will already be set to 10, so a+b will be 20.


@AKMafia001:

I mentioned that parentheses are not binary operators because the term "associativity" only has a meaning for binary operators. See also the wikipedia article on the topic.

Either way what Deitel says is wrong or at the very least they mean something different by "order of evaluation" than we do in this context. Consider for example the following code:

#include<stdio.h>
int main()
{      
        int a=10,b=20;
        a=(a+B)/>-(b=a);
#include<stdio.h>
         
int f() {
    puts("f was called");
    return 4;
}

int g() {
    puts("g was called");
    return 5;
}
        
int main()
{      
    int x = f() + (g() + g());
    return 0;
}



If the code in parentheses was evaluated first, then the above program should print "g was called" twice before it prints "f was called". On my system at least it prints "f was called" first, so clearly f() was evaluated before the expression in parentheses (on your system the order may be different because, as I said, it's not defined, but it's definitely not true that the expression in parentheses needs to be evaluated first).
Was This Post Helpful? 1
  • +
  • -

#14 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 11:33 AM

Thanks Salem and sepp2k. But what i learned yet from books and teachers was that the parentheses changes the order of evaluation. I think i should study this more in depth.
Was This Post Helpful? 0
  • +
  • -

#15 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 11:37 AM

check my last post dude.. and comment if u like the answer
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2