Posted on 2006-04-25
Last Modified: 2010-04-01
here is the following statement

i = 5;
j = i++ - ++i;

What are the possible values of j in C++?

My guess would be 0 because if I increase i then subtract i then i should get 0, am I right?
Question by:shahrine99
    LVL 30

    Assisted Solution

    >>What are the possible values of j in C++?

    In C++ that statement produces undefined behavior (IAW C++ standard).
    So the value can be anything.
    LVL 11

    Accepted Solution

    The answer is ambiguous, but saying it is implementation defined what happens is a far cry from saying it can be anything.
    No compiler writer is going to put a random number in there.

    The question is, when is i++ executed.
    The answer is, sometime after i is used and the semicolon. That means there are two possible answers for the expression.

    Your first step should be to compile a little program and see for yourself. And you're right, zero is one possible answer.

    Suppose it was rewritten:

    j=i - ++i;

    What would the answer be?

    Suppose it was written:
    int temp = i++;
    j = temp - ++i;

    What then?
    LVL 3

    Assisted Solution

    Yes you are right...
    Answer will be zero...

    see how it is processed....

    j = i++ - ++i;
     will be equivalent to
    j = i++ - (Increaced Value of i;
    and now i is 6

    j = i++ - 6;

    now it will be equivalent to

    j= (Value of i - 6; and then Increase i

    so j will be 0 and i will be 7

    LVL 3

    Expert Comment


    >Suppose it was rewritten:

    >j=i - ++i;

    >What would the answer be?

    >Suppose it was written:
    >int temp = i++;
    >j = temp - ++i;

    >What then?

    Then answer will be -2;
    int temp = i++; this will assign the old value 5 to temp and then increase i to 6;

    j = temp - ++i; will be equivalent to

    j = 5 - ++i; and this will be equibalent to

    j = 5 - (Increace value of i);

    and finally

    j= 5 - 7 = -2

    LVL 30

    Expert Comment

    >>The answer is ambiguous, but saying it is implementation defined what happens is a far cry from saying it can be anything.

    Yes, it is a bit of a strech to truely believe that it can equal to anything.
    However, the point I'm trying to make, is that you should not care what it equals to, because you should avoid using implementation defined code, if it's not absolutely required.
    Clearly this type of code usage is not absolutely required, and you can change the code to give you well defined behavior, instead of implementation defined behavior.

    However, technically speaking, implementation defined means the implementation can do anything it wants, and therefore the results can be anything the implementation desires it to be.
    I'm not saying any compiler will do this, but I am saying that a compiler can do it, and still be considered compliant to the C++ standard.
    LVL 11

    Expert Comment

    So there you have it.
    LVL 30

    Expert Comment

    A good example of why you should not use implementation defined behavior, is the modification of a string literal.
    char* data = "Hello World";
    strcpy(data, "Good by");

    The above code results in implementation defined behavior.
    Many years ago, before VC++ 6.0 came out, many season C++ programmers continued to warn developers about using this type of code.
    However, since VC++ 5.0 happly ran above code with no runtime errors, many developers continued to use such implementation defined code.
    When VC++ 6.0 came out, it did not produce the same behavior, and infact it causes a runtime error with above code.
    So developers who try to compile the same code in VC++ 6.0, whould get a runtime error, when they previously did not.

    So when you're dealing with implementation defined code, you not only have to worry about how the code will work from one compiler to the next, but you also have to worry about how the code will work from one version of the compiler to the next.
    Even though VC++ 5.0 and VC++ 6.0 have different results with the above code, both are considered compliant with the standard with their results.

    When developers first started complaining about this issue, one of the first questions they asked, was which compiler is right (5.0 or 6.0)?
    And the answer is both are right, because they can do anything they want with undefined behavior.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now