Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By grumpy

inc/dec operators with scanf

This is a discussion on inc/dec operators with scanf within the C Programming forums, part of the General Programming Boards category; Hi everyone! When I give the value for computation in the program it gives me the right answer,but when I ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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
    shruthi likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    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.
    shruthi likes this.
    Right 98% of the time, and don't care about the other 3%.

  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, 02: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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21