Thread: Help with Pre and Post Increment !

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Try above programm's and i bet they will give same output in any compiler.
    Just go and read the link to my other thread in post #4

    Fed up with this kind of BS from know-nothings, I did an experiment with the same code and many compilers.
    It's the same kind of broken code as you've posted, and the results are DIFFERENT.

    You just lost the bet - pay up and then shut up.

    The C standard tells you you're wrong.

    All the regulars (with a combined programming experience of 100's of years and most of the compilers you're ever likely to see) tell you you're wrong.

    Any sane person might start to question their supposedly infallible source material and start doing a bit more independent research, rather than relying on what one book and one compiler is telling you.
    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.

  2. #17
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Refer to the C standard:

    This means that the order of evaluation of the arguments is unspecified ("indeterminately sequenced"), so your claim of "right from left convention" does not necessarily hold true. But there's more:

    In the code from post #1, the two instances of I++ and the ++I result in side effects on I, which is the same scalar object. These side effects are unsequenced relative to each other since the sequence point comes "after the evaluations of the function designator and the actual arguments but before the actual call". Therefore, the behaviour is undefined. Likewise for a++ and ++a in the example you posted in post #3.


    How much would you like to bet?
    sorry i am i non-English speaker, thus i have to read that c standards many time, but i think i know the that part and i think it is associated with precedence of c operators. I already had read in somewhere but for now i cant remember it. i will give you reply sooner

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    but i think i know the that part and i think it is associated with precedence of c operators.
    Related, but not the same. Precedence specifies grouping, not order of evaluation. This is about order of evaluation and about undefined behaviour when you have operations that are unsequenced relative to each other and yet affect the same object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by Salem View Post
    > Try above programm's and i bet they will give same output in any compiler.
    Just go and read the link to my other thread in post #4

    Fed up with this kind of BS from know-nothings, I did an experiment with the same code and many compilers.
    It's the same kind of broken code as you've posted, and the results are DIFFERENT.


    You just lost the bet - pay up and then shut up.

    The C standard tells you you're wrong.

    All the regulars (with a combined programming experience of 100's of years and most of the compilers you're ever likely to see) tell you you're wrong.

    Any sane person might start to question their supposedly infallible source material and start doing a bit more independent research, rather than relying on what one book and one compiler is telling you.
    yeah its true they give different answers i have tried with code blocks, turbo c and dev c++ they all gave me different answer.
    then i modified the code and declared the variable i outside of main and they all give me same result, thus i think those compiler to deal with variable scope.
    Last edited by raviwagh; 10-29-2014 at 01:25 AM.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Quote Originally Posted by raviwagh View Post
    which compiler are you using??
    The real question is what compiler are you using that isn't warning you about these problems?


    I use gcc by the way, with quite a few warning switches enabled.

    Jim

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    then i modified the code and declared the variable i outside of main and they all give me same result, thus i think those compiler to deal with variable scope.
    More irrelevant nonsense.

    If the same code produces different answers when compiled with different compilers, or produces different answers simply by moving variables around in memory, then for all practical purposes, it means there is a bug in the code.

    Here's another link for you to seemingly ignore.
    Expressions
    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.

  7. #22
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by Salem View Post
    More irrelevant nonsense.

    If the same code produces different answers when compiled with different compilers, or produces different answers simply by moving variables around in memory, then for all practical purposes, it means there is a bug in the code.

    Here's another link for you to seemingly ignore.
    Expressions
    its not bug, int i's storage class is changed by moving the int i outside of the main, and storage classes can give different answers by moving the same code.
    Ok i quit here, i assume you all are right and it is the undefined behavior and it can produce different answer with different compilers.
    But as long as i dig into the books i have to get known thing's that one thing that how to not use such kind's of codes, in the thebenman's programm produce different outputs because the output have to deal with scope of variables, compilers built features, os architecture and sequence of the operators. I think because of all above parts it gave different answers. First of all the compiler is have to solve the answer by the precedence of the operators and if we assume all compiler having same sequence but then also the compilers default variable scope can be different, Thus i advice that to avoid little effort why should we code like that, to increment of a variable there are for loops and while loops and cases which can handle our programm better and give reliable outputs. But i think this programm is also important for knowing the how the code works in the complex programms.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    its not bug, int i's storage class is changed by moving the int i outside of the main, and storage classes can give different answers by moving the same code.
    That is true in the specific case where a variable was not explicitly initialised and then used: if the object has static storage duration, such as by virtue of being global, then it would be zero initialised so its use would be fine. If the object had automatic storage duration instead then its value would be indeterminate. However, other than this, the usage of a variable in an expression should have the same net effect regardless of the object's storage duration, otherwise you have a bug.

    Quote Originally Posted by raviwagh
    the compilers default variable scope can be different
    No, the notion of scope is in the standard. We can always identify the scope of a variable and know that that is how any conforming compiler will treat it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    it is the undefined behavior and it can produce different answer with different compilers.
    There is actually a difference between undefined behaviour and unspecified behaviour:
    Quote Originally Posted by C11 Clause 3.4.3
    undefined behavior
    behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

    NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    EXAMPLE
    An example of undefined behavior is the behavior on integer overflow.
    Quote Originally Posted by C11 Clause 3.4.4
    unspecified behavior
    use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance

    EXAMPLE
    An example of unspecified behavior is the order in which the arguments to a function are evaluated.
    What this means is that if you call a function like this:
    Code:
    f(x(), y());
    then whether x is called first or y is called first is up to the compiler. So, if say you print something to standard output in x and also in y, which gets printed first is up to the compiler. So, this is a possible bug, but it is not undefined behaviour.

    On the other hand, for the code in posts #1 and #3, it is not only unspecified behaviour, but undefined behaviour. This means that although jimblumberg's compiler was nice enough to warn about it, the compiler could well have simply refused to compile.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    but compiler compiled that why is that??

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    but compiler compiled that why is that??
    Perhaps because the check is/was only enabled at a higher warning level, so it would be more consistent to always compile rather than to only compile when warning level is low.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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. pre and post increment
    By prathiksa in forum C Programming
    Replies: 7
    Last Post: 12-07-2012, 06:03 AM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  3. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  4. pre/Post Increment
    By ganesh bala in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 04:17 AM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM