The '++' operator. !!! HELP !!!

This is a discussion on The '++' operator. !!! HELP !!! within the C Programming forums, part of the General Programming Boards category; Hi friends, I am new to the C language and need some help with the increment operator '++'. Please check ...

  1. #1
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Unhappy The '++' operator. !!! HELP !!!

    Hi friends,
    I am new to the C language and need some help with the increment operator '++'. Please check the following code snippet:
    Code:
    int i,a;
    i=0;
    a = i++ + i++;
    printf("\n i++ + i++ = %d",a);
    i=0;
    printf("\n i++ + i++ = %d",i++ + i++);
    The output :
    Code:
    i++ + i++ = 0
    i++ + i++ = 1
    I do not understand the difference in the output of the operator. Please help.
    Thank you.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    in other words, "don't do that"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Unhappy

    i know
    Code:
    i++ + i++
    is bad programming practice because of the ambiguity. I am only a beginner so it confuses me to see such statements and it terrifies me when i realise that i would have to answer similar questions (set to confuse students) in an exam i am about to take towards the end of oct. I want to get such mind blocks out of the way as soon as possible with advanced topics about to come. Please don't mind if ask questions which seem very useless.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not ambiguous, it's undefined behaviour.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    http://c-faq.com/expr/evalorder2.html
    Learn about side effects, modification rules and sequence points.
    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.

Popular pages Recent additions subscribe to a feed

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