1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c input/output

Discussion in 'C' started by state, Jul 29, 2012.

  1. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    #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?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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 likes this.
  3. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Sorry i mistyped the answer.Thank you for replying.
     

Share This Page