Thread: inc/dec operators with scanf

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    inc/dec operators with scanf

    Hi everyone! When I give the value for computation in the program it gives me the right answer,but when I use scanf and enter the value,the result is incorrect. I am using gcc compiler.
    Code:
    //program without scanf
    #include<stdio>
    int main()
    {
        int x=2,y;
        y=x++ + ++x;
        printf("x=%d,y=%d\n");
        return 0;
    }
    
    Output: x=4,y=6 // correct output
     
    //program with scanf
    #include<stdio>
    int main()
    {
        int x,y;
        printf("value for x\n");
         scanf("%d",&x);
        y=x++ + ++x;
        printf("x=%d,y=%d\n");
        return 0;
    }
    Output: x=3,y=5 //incorrect output
    I am not able to find a logical explaination,could anyone help me out here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Comma Operator - C And C++ | Dream.In.Code
    Your code is broken, so getting the "right" answer out of any compiler is a crap shoot.

    Learn all about sequence points.
    Question 3.8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Modifying any variable twice with no intervening sequence points gives undefined behaviour (in other words the C standard does not forbid any potential outcome) which means ANY result is acceptable.

    When behaviour is undefined, reformatting your hard drive or electrocuting you via your USB port are both alternative outcomes that are just as acceptable as producing the output you expect.

    Your code is modifying x twice in one statement, which means there is no sequence point between the two increment operations. Both sets of output you are getting are correct, because all possible results are allowed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operators
    By ujwala in forum C Programming
    Replies: 4
    Last Post: 01-30-2008, 03:25 AM
  2. operators... which gets run first?
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2005, 08:23 PM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. Two + operators
    By shane1985 in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2003, 01:33 PM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM