tricky output ???

Discussion in 'C Programming' started by onkar, Feb 16, 2008.

  1. onkar

    onkar Guest

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


    This gives me : 4 5 5 5 5

    How ???

    Can anyone please give me some hint as to why I am getting this ??

    Thanks & regards,
    Onkar
     
    onkar, Feb 16, 2008
    #1
    1. Advertising

  2. onkar

    onkar Guest

    On Feb 16, 11:17 am, Richard Heathfield <> wrote:
    > onkar said:
    >
    > > #include<stdio.h>
    > > int main(void){
    > > int i=5;
    > > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > > return 0;
    > > }

    >
    > > This gives me : 4 5 5 5 5

    >
    > > How ???

    >
    > > Can anyone please give me some hint as to why I am getting this ??

    >
    > What answer were you expecting, and why?
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999



    I was expecting 4 5 5 4 5
     
    onkar, Feb 16, 2008
    #2
    1. Advertising

  3. onkar said:

    > #include<stdio.h>
    > int main(void){
    > int i=5;
    > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > return 0;
    > }
    >
    >
    > This gives me : 4 5 5 5 5
    >
    > How ???
    >
    > Can anyone please give me some hint as to why I am getting this ??


    What answer were you expecting, and why?


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 16, 2008
    #3
  4. onkar said:

    > On Feb 16, 11:17 am, Richard Heathfield <> wrote:
    >> onkar said:
    >>
    >> > #include<stdio.h>
    >> > int main(void){
    >> > int i=5;
    >> > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    >> > return 0;
    >> > }

    >>
    >> > This gives me : 4 5 5 5 5

    >>
    >> > How ???

    >>
    >> > Can anyone please give me some hint as to why I am getting this ??

    >>
    >> What answer were you expecting, and why?

    >
    > I was expecting 4 5 5 4 5


    From your reply, I deduce that you were expecting the compiler to evaluate
    the printf arguments from right to left. But in fact C doesn't guarantee
    any particular evaluation order for function arguments. To get the output
    you require, do this:

    #include <stdio.h>

    int main(void)
    {
    int i = 5;
    int z = i;
    int y = --i;
    int x = ++i;
    int w = i--;
    int v = i++;
    printf("%d %d %d %d %d\n", v, w, x, y, z);
    return 0;
    }

    This guarantees the output 4 5 5 4 5

    It is always a bad idea to make a line of C code do too much. Break complex
    expressions down into stuff you can actually understand. (If you thought
    you understood printf("%d %d %d %d %d\n",i++,i--,++i,--i,i), it should be
    evident from your surprise at the output that you were mistaken to think
    that.)

    In general, if you're using ++ or -- on any particular object (or indeed
    modifying the object's value in any way), that should be the only place
    you use that object within that whole statement. Otherwise, the behaviour
    of the code is not defined by the language spec.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 16, 2008
    #4
  5. onkar <> writes:
    > #include<stdio.h>
    > int main(void){
    > int i=5;
    > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > return 0;
    > }
    >
    >
    > This gives me : 4 5 5 5 5

    [...]

    The FAQ for this newsgroup is at <http://www.c-faq.com/>.

    You desperately need to read section 3. (Then read the rest of it.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2008
    #5
  6. onkar

    Ian Collins Guest

    onkar wrote:
    > #include<stdio.h>
    > int main(void){
    > int i=5;
    > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > return 0;
    > }
    >

    What's brought all these daft UB questions out of the woodwork this week?

    --
    Ian Collins.
     
    Ian Collins, Feb 16, 2008
    #6
  7. Ian Collins said:

    > onkar wrote:
    >> #include<stdio.h>
    >> int main(void){
    >> int i=5;
    >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    >> return 0;
    >> }
    >>

    > What's brought all these daft UB questions out of the woodwork this week?


    What makes you think there's anything special about this week? :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 16, 2008
    #7
  8. onkar wrote:
    > #include<stdio.h>
    > int main(void){
    > int i=5;
    > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > return 0;
    > }
    >
    >
    > This gives me : 4 5 5 5 5
    >
    > How ???
    >
    > Can anyone please give me some hint as to why I am getting this ??


    Have you not been paying attention? How many times do we have to answer
    the same half-assed question here? Not only is this covered in three
    separate questions in the FAQ, but

    This post, with the same stupidity,
    From: "" <>
    Subject: Funny results with increment decrement operators
    Date: Fri, 15 Feb 2008 16:00:33 -0800 (PST)

    was posted (and answered) only 6 hours before yours

    From: onkar <>
    Subject: tricky output ???
    Date: Fri, 15 Feb 2008 22:09:44 -0800 (PST)
     
    Martin Ambuhl, Feb 16, 2008
    #8
  9. onkar

    Bartc Guest

    "Martin Ambuhl" <> wrote in message
    news:...
    > onkar wrote:


    >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);


    > This post, with the same stupidity,
    > From: "" <>
    > Subject: Funny results with increment decrement operators
    > Date: Fri, 15 Feb 2008 16:00:33 -0800 (PST)
    >
    > was posted (and answered) only 6 hours before yours


    And in comp.programming a week ago.

    It can only be a favourite assignment of C/programming classes.

    --
    Bart
     
    Bartc, Feb 16, 2008
    #9
  10. onkar

    Randy Howard Guest

    On Sat, 16 Feb 2008 01:04:31 -0600, Ian Collins wrote
    (in article <>):

    > onkar wrote:
    >> #include<stdio.h>
    >> int main(void){
    >> int i=5;
    >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    >> return 0;
    >> }
    >>

    > What's brought all these daft UB questions out of the woodwork this week?


    New semester started recently.



    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Feb 16, 2008
    #10
  11. onkar

    Richard Guest

    Richard Heathfield <> writes:

    > onkar said:
    >
    >> #include<stdio.h>
    >> int main(void){
    >> int i=5;
    >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    >> return 0;
    >> }
    >>
    >>
    >> This gives me : 4 5 5 5 5
    >>
    >> How ???
    >>
    >> Can anyone please give me some hint as to why I am getting this ??

    >
    > What answer were you expecting, and why?


    it should be blatantly obvious why he doesn't think this is right. The
    fact he would be wrong is besides the point.
     
    Richard, Feb 16, 2008
    #11
  12. onkar

    santosh Guest

    onkar wrote:

    > #include<stdio.h>
    > int main(void){
    > int i=5;
    > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > return 0;
    > }
    >
    >
    > This gives me : 4 5 5 5 5
    >
    > How ???
    >
    > Can anyone please give me some hint as to why I am getting this ??
    >
    > Thanks & regards,
    > Onkar


    This guy is doubtless a troll. He keeps posting such stupid questions
    even after months of being told about the FAQ.
     
    santosh, Feb 17, 2008
    #12
  13. Richard wrote:
    >
    > Richard Heathfield <> writes:
    >
    > > onkar said:
    > >
    > >> #include<stdio.h>
    > >> int main(void){
    > >> int i=5;
    > >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > >> return 0;
    > >> }
    > >>
    > >>
    > >> This gives me : 4 5 5 5 5
    > >>
    > >> How ???
    > >>
    > >> Can anyone please give me some hint as to why I am getting this ??

    > >
    > > What answer were you expecting, and why?

    >
    > it should be blatantly obvious why he doesn't think this is right. The
    > fact he would be wrong is besides the point.


    But, until one starts to think about "why" he thinks the answer
    should be what he thinks it should be, you cannot explain to him
    the flaws in his thinking.

    --
    +-------------------------+--------------------+-----------------------+
    | 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, Feb 17, 2008
    #13
  14. Ian Collins wrote:
    >
    > onkar wrote:
    > > #include<stdio.h>
    > > int main(void){
    > > int i=5;
    > > printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    > > return 0;
    > > }
    > >

    > What's brought all these daft UB questions out of the woodwork this week?


    Perhaps a new semester started a few weeks ago, and the instructors
    keep on bringing out the same tired "let's invoke UB" questions?

    What I want to know is why the instructors ask these questions? I
    somehow doubt that most of them are trying to teach about UB, but
    instead are assuming that "all the world's a VAX". (Or whatever is
    the 21st century equivalent. Perhaps "all the world's a Windows
    system using MSVC"?)

    --
    +-------------------------+--------------------+-----------------------+
    | 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, Feb 17, 2008
    #14
  15. Kenneth Brody said:

    > Richard wrote:
    >>
    >> Richard Heathfield <> writes:
    >>
    >> > onkar said:
    >> >
    >> >> #include<stdio.h>
    >> >> int main(void){
    >> >> int i=5;
    >> >> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
    >> >> return 0;
    >> >> }
    >> >>
    >> >>
    >> >> This gives me : 4 5 5 5 5
    >> >>
    >> >> How ???
    >> >>
    >> >> Can anyone please give me some hint as to why I am getting this ??
    >> >
    >> > What answer were you expecting, and why?

    >>
    >> it should be blatantly obvious why he doesn't think this is right. The
    >> fact he would be wrong is besides the point.

    >
    > But, until one starts to think about "why" he thinks the answer
    > should be what he thinks it should be, you cannot explain to him
    > the flaws in his thinking.


    In fact, his answer to my question gave me a clue as to what he was
    thinking. He might equally have said: 5 6 6 5 5, in which case my answer
    would have been slightly different.

    As usual, Richard Riley is sniping ignorantly, caustically, and
    uncomprehendingly at people who are actually supplying help, instead of
    taking the risk of supplying help himself to those who are asking for it.
    He may safely be ignored, since he never contributes anything useful.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 17, 2008
    #15
    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. Jim Schueler

    Tricky AUTOLOAD behavior

    Jim Schueler, Aug 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    443
  2. VB Programmer

    Tricky Question: Page_Load

    VB Programmer, Jun 30, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    336
    VB Programmer
    Jun 30, 2003
  3. Matt

    Tricky postback

    Matt, Aug 8, 2003, in forum: ASP .Net
    Replies:
    8
    Views:
    1,675
    sivanekar
    Sep 17, 2007
  4. VB Programmer
    Replies:
    4
    Views:
    2,178
    Natty Gur
    Aug 13, 2003
  5. Replies:
    9
    Views:
    541
    CBFalconer
    Apr 25, 2006
Loading...

Share This Page