c input/output

Go4Expert Member
29Jul2012,08:21   #1
state's Avatar
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
a = -3- -3;
b = -3 - - (-3 );
printf("%d %d",a,b);
getch();
return 0 ;
}


the ouput is -6 and 0.Please explain how?
Mentor
30Jul2012,00:13   #2
xpi0t0s's Avatar
Compiler bug probably. (-3) - (-3)=0 because any finite number minus itself is zero, so if your compiler thinks this is -6 then it's wrong. Similarly (-3) - -(-3) = (-3) - (3) = -6, not 0.

The answers are correct though, if transposed, so if the output is 0 -6 then that's correct. -6 0 would be wrong. To make certain, split the output with more detail to be sure:
Code:
printf("(-3) - (-3) = %d\n",a);
printf("(-3) - -(-3) = %d\n",b);
Visual Studio 2010 gets the correct answers.
Code:
void test49()
{
	int a,b;
	a = -3- -3;
	b = -3 - - (-3 );
	printf("%d %d\n",a,b);
	printf("-3- -3=%d\n",a);
	printf("-3 - - (-3 )=%d\n",b);
}
Output:
Code:
0 -6
-3- -3=0
-3 - - (-3 )=-6
The bottom line really is to stop using Turbo C 3.0, since it was written in 1842 by a Japanese caveman who had never seen a computer.
state like this
Go4Expert Member
30Jul2012,15:14   #3
state's Avatar
Sorry i mistyped the answer.Thank you for replying.