Increment Operator

This is a discussion on Increment Operator within the C Programming forums, part of the General Programming Boards category; Can anyone please explain me the following results? int y,x; x=4; y=x+ ++x + ++x; printf("%d",y); The output comes out ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    Post Increment Operator

    Can anyone please explain me the following results?

    int y,x;
    x=4;
    y=x+ ++x + ++x;
    printf("%d",y);


    The output comes out to be 16. How?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I'm pretty sure that this sort of behaviour is undefined by the C standard, and so your compiler can make the answer anything it well pleases.

    Don't use use the increment operator on the same variable twice in the same expression.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pretty straight forward, the increment has precedence (both of them), so it's 6 + 4 + 6.

    All these ++--++-- stuff, when they have the same sequence point, are baloney, compiler specific, and probably undefined, as well.

    Don't use them on the same variable, in the same line of code.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Google keyword: sequence point.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread


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