increment operator misbehaving

This is a discussion on increment operator misbehaving within the C Programming forums, part of the General Programming Boards category; int ppi = 10,ppj=20,ppk=30,ppl =40; Code: printf("\n\nOriginally ppi = %d",ppi); printf("\nPassed ppi = %d , and ppi++ = %d", ppi, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    increment operator misbehaving

    int ppi = 10,ppj=20,ppk=30,ppl =40;
    Code:
    	printf("\n\nOriginally ppi = %d",ppi);
            printf("\nPassed ppi = %d , and ppi++ = %d", ppi, ppi++);
    	printf("\nFinally ppi = %d",ppi);
    
    	printf("\n\nOriginally ppj = %d",ppj);
            printf("\nPassed ppj = %d , and ++ppj = %d", ppj, ++ppj);
    	printf("\nFinally ppj = %d",ppj);
    
    	printf("\n\nOriginally ppk = %d",ppk);
            printf("\n++ppk = %d , ppk = %d", ++ppk, ppk);
    	printf("\nFinally ppk = %d",ppk);
    
    	printf("\n\nOriginally ppl = %d",ppl);
            printf("\nppl++ = %d , ppl = %d", ppl++, ppl);
    	printf("\nFinally ppl = %d",ppl);
    Output:

    Originally ppi = 10
    Passed ppi = 11 , and ppi++ = 10
    Finally ppi = 11

    Originally ppj = 20
    Passed ppj = 21 , and ++ppj = 21
    Finally ppj = 21

    Originally ppk = 30
    ++ppk = 31 , ppk = 31
    Finally ppk = 31

    Originally ppl = 40
    ppl++ = 40 , ppl = 41
    Finally ppl = 41

    ppi and ppj appear to be behaving to the proper function passing conventions, but ppk and ppl appear to be misbehaving.. Can anyone pls help me!!!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    The C standard doesn't specify which order function arguments are evaluated - it's completely implementation-defined.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Using ++ more than once in any statement is undefined behavior. You must use it a maximum of once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Elysia
    Using ++ more than once in any statement is undefined behavior. You must use it a maximum of once.
    That is not true. For example, the operands could be entirely unrelated variables, or there could be a sequence point between the usage in the same statement.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Ah, bad phrasing.
    Modifying the same variable twice or more within a sequence point (statement?) is undefined behavior, it is, I believe?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Thanks guys, for pitching in..
    I dont think, it can be completely random though..
    It has to follow a pattern, right to left or left to right..
    Above appears that compiler is generating a random code?? How can it be.. Am I missing something??

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It is random. The compiler can choose its own way of handling it, so there is no "standard" way of doing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Thanks guys.. I will try to find out more abt it.. thanks a lot

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Elysia
    Modifying the same variable twice or more within a sequence point (statement?) is undefined behavior, it is, I believe?
    Yes, though in this case I think the second part of that rule, that "the prior value shall be read only to determine the value to be stored", along with the rule that the order of evaluation of arguments is unspecified, comes into play. The former would result in undefined behaviour, so I suppose that the latter is just a matter of "academic interest".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 09:01 AM
  2. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 08:24 PM
  3. increment IP addresses - newbie Q.
    By webwesen in forum C Programming
    Replies: 6
    Last Post: 09-09-2003, 08:25 AM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:01 AM

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