Increment operator

Discussion in 'C Programming' started by deepak, Jul 26, 2007.

  1. deepak

    deepak Guest

    Hi,

    For the following program the output I'm getting is not the one I
    expected.

    main()
    {
    int i = 3;

    i = i++;

    printf("%d\n", i);
    }

    Output is 3 and the output I expected was 4.
    Is it an undefined behaviour?

    Thanks,
    Deepak
     
    deepak, Jul 26, 2007
    #1
    1. Advertising

  2. deepak

    Ian Collins Guest

    deepak wrote:
    > Hi,
    >
    > For the following program the output I'm getting is not the one I
    > expected.
    >
    > main()
    > {
    > int i = 3;
    >
    > i = i++;
    >
    > printf("%d\n", i);
    > }
    >
    > Output is 3 and the output I expected was 4.
    >

    Why would you expect 4? The result of i++ is the original value of i.

    --
    Ian Collins.
     
    Ian Collins, Jul 26, 2007
    #2
    1. Advertising

  3. deepak

    Ben Pfaff Guest

    deepak <> writes:

    > i = i++;


    This is in the FAQ.

    3.3: I've experimented with the code

    int i = 3;
    i = i++;

    on several compilers. Some gave i the value 3, and some gave 4.
    Which compiler is correct?

    A: There is no correct answer; the expression is undefined. See
    questions 3.1, 3.8, 3.9, and 11.33. (Also, note that neither
    i++ nor ++i is the same as i+1. If you want to increment i,
    use i=i+1, i+=1, i++, or ++i, not some combination. See also
    question 3.12.)

    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Jul 26, 2007
    #3
  4. Ian Collins said:

    > deepak wrote:
    >> Hi,
    >>
    >> For the following program the output I'm getting is not the one I
    >> expected.
    >>
    >> main()
    >> {
    >> int i = 3;
    >>
    >> i = i++;
    >>
    >> printf("%d\n", i);
    >> }
    >>
    >> Output is 3 and the output I expected was 4.
    >>

    > Why would you expect 4? The result of i++ is the original value of i.


    Yes, which is a good argument for expecting 3, but presumably the OP's
    expectation was that the program's undefined behaviour would result in
    a value that we might not expect - and boy, was he surprised!

    Undefined behaviour is not /required/ to be bizarre behaviour.
    Incidentally, I ran the program at home and got the output: "a
    suffusion of yellow".

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 26, 2007
    #4
  5. deepak

    Richard Guest

    Richard Heathfield <> writes:

    > Ian Collins said:
    >
    >> deepak wrote:
    >>> Hi,
    >>>
    >>> For the following program the output I'm getting is not the one I
    >>> expected.
    >>>
    >>> main()
    >>> {
    >>> int i = 3;
    >>>
    >>> i = i++;
    >>>
    >>> printf("%d\n", i);
    >>> }
    >>>
    >>> Output is 3 and the output I expected was 4.
    >>>

    >> Why would you expect 4? The result of i++ is the original value of
    >> i.


    Aha. But he didnt print the result of "i++". He printed "i" :-;

    >
    > Yes, which is a good argument for expecting 3, but presumably the OP's
    > expectation was that the program's undefined behaviour would result in
    > a value that we might not expect - and boy, was he surprised!
    >
    > Undefined behaviour is not /required/ to be bizarre behaviour.
    > Incidentally, I ran the program at home and got the output: "a
    > suffusion of yellow".


    No you didn't. You got 3 or 4.
     
    Richard, Jul 26, 2007
    #5
  6. Richard said:

    > Richard Heathfield <> writes:
    >

    <snip>

    >> Incidentally, I ran the program at home and got the output: "a
    >> suffusion of yellow".

    >
    > No you didn't. You got 3 or 4.


    No I didn't. 3 or 4 is 7, and I certainly didn't get 7.

    Anyway, since you accuse me of lying, perhaps I should demonstrate that
    I am not.

    me@here/ub> cat ub.c
    main()
    {
    int i = 3;

    i = i++;

    printf("%d\n", i);
    }

    me@here/ub> compile ub.c
    me@here/ub> ./ub
    a suffusion of yellow
    me@here/ub>

    There are more things in heaven and earth, Horatio, than are dreamt of
    in your philosophy.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 26, 2007
    #6
  7. deepak

    Richard Guest

    Richard Heathfield <> writes:

    > Richard said:
    >
    >> Richard Heathfield <> writes:
    >>

    > <snip>
    >
    >>> Incidentally, I ran the program at home and got the output: "a
    >>> suffusion of yellow".

    >>
    >> No you didn't. You got 3 or 4.

    >
    > No I didn't. 3 or 4 is 7, and I certainly didn't get 7.
    >
    > Anyway, since you accuse me of lying, perhaps I should demonstrate that
    > I am not.
    >
    > me@here/ub> cat ub.c
    > main()
    > {
    > int i = 3;
    >
    > i = i++;
    >
    > printf("%d\n", i);
    > }
    >
    > me@here/ub> compile ub.c
    > me@here/ub> ./ub
    > a suffusion of yellow
    > me@here/ub>
    >
    > There are more things in heaven and earth, Horatio, than are dreamt of
    > in your philosophy.


    Wow. I am convinced. I apologise and take back all those things I said
    about you :-;

    Unless of course your "undefined" compile command got up to some
    trickery and actually created another file called "ub".

    Hmmm.
     
    Richard, Jul 26, 2007
    #7
  8. Richard said:

    > Richard Heathfield <> writes:
    >
    >> Richard said:
    >>
    >>> Richard Heathfield <> writes:
    >>>

    >> <snip>
    >>
    >>>> Incidentally, I ran the program at home and got the output: "a
    >>>> suffusion of yellow".
    >>>
    >>> No you didn't. You got 3 or 4.

    >>
    >> No I didn't. 3 or 4 is 7, and I certainly didn't get 7.
    >>
    >> Anyway, since you accuse me of lying, perhaps I should demonstrate
    >> that I am not.
    >>
    >> me@here/ub> cat ub.c
    >> main()
    >> {
    >> int i = 3;
    >>
    >> i = i++;
    >>
    >> printf("%d\n", i);
    >> }
    >>
    >> me@here/ub> compile ub.c
    >> me@here/ub> ./ub
    >> a suffusion of yellow
    >> me@here/ub>
    >>
    >> There are more things in heaven and earth, Horatio, than are dreamt
    >> of in your philosophy.

    >
    > Wow. I am convinced. I apologise and take back all those things I said
    > about you :-;


    Yeah, right.

    > Unless of course your "undefined" compile command got up to some
    > trickery and actually created another file called "ub".
    >
    > Hmmm.


    No, it was a shell script to invoke a C compiler, which compiled the
    code as written. The resulting executable image was run, and it
    produced the output cited above. I wrote the shell script only so that
    I could avoid mentioning the (conforming) compiler I used, so that I
    could in turn avoid accusations of plugging a particular compiler.

    The output is genuine.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 26, 2007
    #8
  9. deepak

    Manuel T Guest

    Ben Pfaff wrote:

    > char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
    > b[]
    >

    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    > =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
    > 0;for(p--;i--;i--)case+ 2:{i++;if(i)break;else default:continue;if(0)case
    > 1:putchar(a[i&15]);break;}}}


    Just another C hacker.

    Great!
     
    Manuel T, Jul 26, 2007
    #9
  10. deepak

    Chris Dollin Guest

    Manuel T wrote:

    > Ben Pfaff wrote:
    >
    >> char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
    >> b[]
    >>

    > ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    >> =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
    >> 0;for(p--;i--;i--)case+ 2:{i++;if(i)break;else default:continue;if(0)case
    >> 1:putchar(a[i&15]);break;}}}

    >
    > Just another C hacker.


    We're /all/ "just another C hacker": I don't see why you're making a fuss
    about Ben's /signature/.

    --
    Chris "just another hacker" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Jul 26, 2007
    #10
  11. In article <>, Richard <> wrote:
    ....
    >> Undefined behaviour is not /required/ to be bizarre behaviour.
    >> Incidentally, I ran the program at home and got the output: "a
    >> suffusion of yellow".

    >
    >No you didn't. You got 3 or 4.


    (Dicky is an) Alberto Gonzales in training.
     
    Kenny McCormack, Jul 26, 2007
    #11
  12. Chris Dollin <> writes:
    > Manuel T wrote:
    >> Ben Pfaff wrote:
    >>> char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
    >>> b[]
    >>>

    >> ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    >>> =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
    >>> 0;for(p--;i--;i--)case+ 2:{i++;if(i)break;else default:continue;if(0)case
    >>> 1:putchar(a[i&15]);break;}}}

    >>
    >> Just another C hacker.

    >
    > We're /all/ "just another C hacker": I don't see why you're making a fuss
    > about Ben's /signature/.


    In the Perl newsgroups, there's a tradition of similarly short and
    obuscated Perl programs that print the string "Just another Perl
    hacker". Manuel T was probably referring to that. (And let's not
    waste time arguing whether Manuel's comment, or mine, was worth the
    bandwidth.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 26, 2007
    #12
  13. deepak

    Chris Dollin Guest

    Keith Thompson wrote:

    > Chris Dollin <> writes:
    >> Manuel T wrote:
    >>> Ben Pfaff wrote:
    >>>> char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
    >>>> b[]
    >>>>
    >>> ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    >>>> =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
    >>>> 0;for(p--;i--;i--)case+ 2:{i++;if(i)break;else default:continue;if(0)case
    >>>> 1:putchar(a[i&15]);break;}}}
    >>>
    >>> Just another C hacker.

    >>
    >> We're /all/ "just another C hacker": I don't see why you're making a fuss
    >> about Ben's /signature/.

    >
    > In the Perl newsgroups, there's a tradition of similarly short and
    > obuscated Perl programs that print the string "Just another Perl
    > hacker". Manuel T was probably referring to that. (And let's not
    > waste time arguing whether Manuel's comment, or mine, was worth the
    > bandwidth.)


    Ah -- I see -- sorry, Manuel, I misread your comment as derogatory.
    Apologies.

    --
    Chris "oh, look, egg -" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Jul 26, 2007
    #13
  14. Richard Heathfield wrote:
    >
    > Richard said:
    >
    > > Richard Heathfield <> writes:
    > >

    > <snip>
    >
    > >> Incidentally, I ran the program at home and got the output: "a
    > >> suffusion of yellow".

    > >
    > > No you didn't. You got 3 or 4.

    >
    > No I didn't. 3 or 4 is 7, and I certainly didn't get 7.

    [...]

    Strange... I thought 3 or 4 is 1?

    ==========
    #include <stdio.h>

    int main(void)
    {
    int i;
    i = ( 3 || 4 );
    printf("3 or 4 is %d\n",i);
    exit(0);
    }
    ==========
    Output:

    3 or 4 is 1

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Jul 26, 2007
    #14
  15. deepak

    Manuel T Guest

    Chris Dollin wrote:

    > Apologies.


    Don't worry. I've just been curious about that signature and I compiled to
    see it...
     
    Manuel T, Jul 26, 2007
    #15
  16. Kenneth Brody said:

    > Richard Heathfield wrote:
    >>
    >> Richard said:
    >>
    >> > Richard Heathfield <> writes:
    >> >

    >> <snip>
    >>
    >> >> Incidentally, I ran the program at home and got the output: "a
    >> >> suffusion of yellow".
    >> >
    >> > No you didn't. You got 3 or 4.

    >>
    >> No I didn't. 3 or 4 is 7, and I certainly didn't get 7.

    > [...]
    >
    > Strange... I thought 3 or 4 is 1?


    It is, except when it's 7.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 26, 2007
    #16
  17. On Jul 26, 12:40 pm, Kenneth Brody <> wrote:
    > Richard Heathfield wrote:
    >
    > > Richard said:

    >
    > > > Richard Heathfield <> writes:

    >
    > > <snip>

    >
    > > >> Incidentally, I ran the program at home and got the output: "a
    > > >> suffusion of yellow".

    >
    > > > No you didn't. You got 3 or 4.

    >
    > > No I didn't. 3 or 4 is 7, and I certainly didn't get 7.

    >
    > [...]
    >
    > Strange... I thought 3 or 4 is 1?
    >
    > ==========
    > #include <stdio.h>


    #include <stdlib.h>

    > int main(void)
    > {
    > int i;
    > i = ( 3 || 4 );


    i = ( 3 | 4 );

    > printf("3 or 4 is %d\n",i);
    > exit(0);
    > }
    > ==========
    > Output:
    >
    > 3 or 4 is 1


    3 or 4 is 7

    Robert Gamble
     
    Robert Gamble, Jul 26, 2007
    #17
  18. deepak

    Eric Sosman Guest

    Richard Heathfield wrote On 07/26/07 05:18,:
    > Ian Collins said:
    >
    >
    >>deepak wrote:
    >>
    >>>Hi,
    >>>
    >>>For the following program the output I'm getting is not the one I
    >>>expected.
    >>>
    >>>main()
    >>>{
    >>> int i = 3;
    >>>
    >>> i = i++;
    >>>
    >>> printf("%d\n", i);
    >>>}
    >>>
    >>>Output is 3 and the output I expected was 4.
    >>>

    >>
    >>Why would you expect 4? The result of i++ is the original value of i.

    >
    >
    > Yes, which is a good argument for expecting 3, but presumably the OP's
    > expectation was that the program's undefined behaviour would result in
    > a value that we might not expect - and boy, was he surprised!
    >
    > Undefined behaviour is not /required/ to be bizarre behaviour.
    > Incidentally, I ran the program at home and got the output: "a
    > suffusion of yellow".


    I'd hazard a guess it was in the vicinity of the loins,
    and that your dry-cleaning bill is outrageous.

    --
     
    Eric Sosman, Jul 26, 2007
    #18
  19. Eric Sosman said:

    > Richard Heathfield wrote On 07/26/07 05:18,:
    >> Ian Collins said:
    >>
    >>
    >>>deepak wrote:
    >>>
    >>>>Hi,
    >>>>
    >>>>For the following program the output I'm getting is not the one I
    >>>>expected.
    >>>>
    >>>>main()
    >>>>{
    >>>> int i = 3;
    >>>>
    >>>> i = i++;
    >>>>
    >>>> printf("%d\n", i);
    >>>>}
    >>>>
    >>>>Output is 3 and the output I expected was 4.
    >>>>
    >>>
    >>>Why would you expect 4? The result of i++ is the original value of
    >>>i.

    >>
    >>
    >> Yes, which is a good argument for expecting 3, but presumably the
    >> OP's expectation was that the program's undefined behaviour would
    >> result in a value that we might not expect - and boy, was he
    >> surprised!
    >>
    >> Undefined behaviour is not /required/ to be bizarre behaviour.
    >> Incidentally, I ran the program at home and got the output: "a
    >> suffusion of yellow".

    >
    > I'd hazard a guess it was in the vicinity of the loins,
    > and that your dry-cleaning bill is outrageous.


    The mind boggleth, doth it not? But no, the program really did write the
    text "a suffusion of yellow" (and a newline character) to stdout. I
    think my compiler must have been reading too much Douglas Adams
    recently, but in any case that really really is the result that the
    program produced.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 26, 2007
    #19
  20. Richard Heathfield <> writes:
    > Eric Sosman said:
    >> Richard Heathfield wrote On 07/26/07 05:18,:
    >>> Ian Collins said:
    >>>>deepak wrote:
    >>>>>For the following program the output I'm getting is not the one I
    >>>>>expected.
    >>>>>
    >>>>>main()
    >>>>>{
    >>>>> int i = 3;
    >>>>>
    >>>>> i = i++;
    >>>>>
    >>>>> printf("%d\n", i);
    >>>>>}
    >>>>>
    >>>>>Output is 3 and the output I expected was 4.

    [...]
    >>> Undefined behaviour is not /required/ to be bizarre behaviour.
    >>> Incidentally, I ran the program at home and got the output: "a
    >>> suffusion of yellow".

    >>
    >> I'd hazard a guess it was in the vicinity of the loins,
    >> and that your dry-cleaning bill is outrageous.

    >
    > The mind boggleth, doth it not? But no, the program really did write the
    > text "a suffusion of yellow" (and a newline character) to stdout. I
    > think my compiler must have been reading too much Douglas Adams
    > recently, but in any case that really really is the result that the
    > program produced.


    Just out of curiosity, what output does your implementation produce
    for the following?

    #include <stdio.h>
    int main(void)
    {
    int i = 3;
    if (0) {
    i = i++;
    }
    printf("%d\n", i);
    return 0;
    }


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 26, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSDousti

    c++ unary increment operator

    MSDousti, Nov 1, 2003, in forum: C++
    Replies:
    1
    Views:
    457
    Mike Wahler
    Nov 1, 2003
  2. Replies:
    104
    Views:
    11,131
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,544
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    927
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    576
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page