Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   c input/output (http://www.go4expert.com/forums/c-input-output-t28802/)

 state 29Jul2012 08:21

c input/output

#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?

 xpi0t0s 30Jul2012 00:13

Re: c input/output

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 30Jul2012 15:14

Re: c input/output