analyze the answer for the code

This is a discussion on analyze the answer for the code within the C Programming forums, part of the General Programming Boards category; Code: main() { int i=5,j=5,p,q; p=(i++)+(i++)+(i++); q=(++j)+(++j)+(++j); printf("%d,%d,%d,%d",p,q,i,j); } I ran the program and it is showed 22 for q. ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    analyze the answer for the code

    Code:
    main()
    {
    int i=5,j=5,p,q;
    p=(i++)+(i++)+(i++);
    q=(++j)+(++j)+(++j);
    printf("%d,%d,%d,%d",p,q,i,j);
    }
    I ran the program and it is showed 22 for q. How can I get 22 for q? Thank you very much.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Interesting. I get 22 as well. Coding

    a = ++j ;
    b = ++j ;
    c = ++j ;
    z = a+b+c ;

    I get 21, which is what I would have expected with the original (6 + 7 + 8).
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    But the solution on the book is given 24, which is 8+8+8. It is weird.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I would have guessed 24 as my second guess!

    Perhaps this is one of those "undefined behavior" situations.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Disassembling the following code using gcc:
    Code:
    int j=5;
    int q=(++j)+(++j)+(++j)
    results in:
    Code:
    ...
    movl   $0x5,0xfffffffc(%ebp)
    lea    0xfffffffc(%ebp),%eax
    incl   (%eax)
    lea    0xfffffffc(%ebp),%eax
    incl   (%eax)
    mov    0xfffffffc(%ebp),%eax
    mov    0xfffffffc(%ebp),%edx
    add    %eax,%edx
    lea    0xfffffffc(%ebp),%eax
    incl   (%eax)
    mov    %edx,%eax
    add    0xfffffffc(%ebp),%eax
    ...
    Leading, if I'm not wrong, to something like 7+7+8 (=22).
    So the evaluation is probably indeed undefined and thus the case 8+8+8 is an acceptable alternative.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, this question crops up every now and again, and it's just as undefined this time as last time someone asked (although that time it was about x++*x++*x++).

    The compiler is allowed to "do as it likes" with all those ++ calculations, and some compilers will perform this in a different way than others. Realistically, any answer between 15 and 24 is likely, and since it's in the "undefined behaviour land" (which is a bit like Wonderland that Alice went to), you could also get completely and utterly rubbish answers.

    If you really want to do something like that, then you should write it out as you want it done, e.g.
    Code:
     
    q = j+1 + j+2 + j+3;
    // Which can be simplified as:
    q = j*3 + 6;
    // Which becomes:
    q = 15+6 -> 21.
    And by the way, when optimizing this code in gcc (gcc -O2 -S x.c):
    Code:
      int j=5;
      int q=(++j)+(++j)+(++j);
      printf("q = %d, j = %d\n", q, j);
    it comes up with 22:
    Code:
            movl    $8, %edx
            movl    $22, %eax
            movl    %edx, 8(%esp)
            movl    %eax, 4(%esp)
            call    _printf
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 08:35 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

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