-2

Possible Duplicate:
Undefined Behavior and Sequence Points

int a=10;

a=a++;


a=++a;


a=(a++);

Can you guys please explain to me why none of these cases works?

Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • 12
    They all work, they just don't work the way you expect. – Peter Lawrey Nov 11 '11 at 14:07
  • 2
    http://stackoverflow.com/q/4176328/20984 – Luc Touraille Nov 11 '11 at 14:07
  • 4
    @PeterLawrey: what do you mean by "They all work"? A code with undefined behavior can hardly be said to "work". – Luc Touraille Nov 11 '11 at 14:10
  • I just saw that the question was tagged `java` in addition to `C++`. If you are interested in answers for both languages, you should clarify that in your question (and probably remove the use of `cout` which make it seems like this question is only about c++). – Luc Touraille Nov 11 '11 at 14:12
  • @LucTouraille I think he means that they all behave as would be expected from the code, thus for the OP it is the expected behaviour that is wrong and not the code. – kinofrost Nov 11 '11 at 14:13
  • @LucTouraille, It is defined in `Java`, I forgot it wasn't defined in `C++` Perhaps in a later spec... – Peter Lawrey Nov 11 '11 at 14:13
  • oops, i actualy wrote the code in c++ and tried to run in codepad.org , but it gave me warning/error and din work! while writing the question, i felt like its general in java and c++ , so i tagged them like that, nyways correctd :) – COD3BOY Nov 11 '11 at 14:14
  • @PeterLawrey: Yes, sorry about the comment, I missed the `java` tag... – Luc Touraille Nov 11 '11 at 14:15
  • @PeterLawrey can you guys please say why it gives error when I run this in codepad.org (coded in c++) ? – COD3BOY Nov 11 '11 at 14:19
  • @Sanjay, What is the error? Perhaps it give a hint. ;) – Peter Lawrey Nov 11 '11 at 14:25
  • -1: When I try to run any C++ code on the web site I get "Internal Server Error" You need to ask the people supporting the site as to why it is down. It has nothing to do with the code you are running. – Peter Lawrey Nov 11 '11 at 14:35
  • Oh, it's Codepad once again. That unreliable thing that pukes errors on just about anything in any language. – BoltClock Nov 11 '11 at 14:38
  • @PeterLawrey Why -1 for "Internal Server Error" , i'm also not responsible for that ! :D btw, the error is "operation on 'a' may be undefined" , here is the link : http://codepad.org/cYXEuRuQ – COD3BOY Nov 11 '11 at 14:43
  • Ok I take of -1 for the Internal error and add -1 for not stating that the WARNING (which has nothing to do with it working or not) is that you are trying to use an operation with an undefined behaviour, something we have been trying to tell you as well. The compiler is being run with an option to treat warnings as errors, which it also clearly states. This is why you need to read messages carefully. If you want to know what they mean, state exactly what they say, not in terms even you don't understand. – Peter Lawrey Nov 11 '11 at 14:48
  • How about READING the error message? "Operation on a may be undefined" is pretty clear to me - and correct at that.. – Voo Nov 11 '11 at 14:50
  • @PeterLawrey Dear lord! I messed up a lot, and now I understood the thing! I was not able to run it in codepad, and I was eager to know why this may be an undefined behavior.I din think much and thought this might be the same with java and tagged such,my bad. – COD3BOY Nov 11 '11 at 14:56
  • @PeterLawrey i mentioned in one of my previous comment "but it gave me warning/error" in which I meant It gave me a warning that turned out to be an error for me! :D never mind! – COD3BOY Nov 11 '11 at 15:06
  • @Sanjay, We all make mistakes and we are all learning. Hopefully more learning than making mistakes. ;) – Peter Lawrey Nov 11 '11 at 15:44
  • @PeterLawrey lol I learnt a lot today! can you guys help me to close this, so that I dont get anymore downvotes! :D – COD3BOY Nov 11 '11 at 16:00
  • @PeterLawrey my bad, I initially tagged it as java, now I tagged it only c++, in this case I cannot accept your answer! Will you please edit your answer as in case c++ , so that I can accept the answer! – COD3BOY Nov 11 '11 at 16:02

4 Answers4

5

Use it the right way:

int a = 10;
a++;
cout << a;
++a;
cout << a;
Michael S.
  • 589
  • 8
  • 25
1

In answer to the C++ question for this code at http://codepad.org/cYXEuRuQ

#include<iostream.h>
int main()
{
int a=10;
a=a++;
cout<<a;
cout<<"\n";
a=++a;
cout<<a;
cout<<"\n";
a=(a++);
cout<<a;
cout<<"\n";
}

when compiled prints

cc1plus: warnings being treated as errors
In function 'int main()':
Line 5: warning: operation on 'a' may be undefined
Line 8: warning: operation on 'a' may be undefined
Line 11: warning: operation on 'a' may be undefined

This is a warning stating that the operation used is undefined and should not be used if possible. This is because in C++ the order of evaluation of ++ relative to other expressions is not defined and not the same across all compilers. (Generally it doesn't matter and is not a problem except in edge cases like these)

The web site goes further and treats warnings as errors and does not run the code.


If you translate to Java it prints

10
11
11

as expected. What do you mean by "doesn't work"?

The behaviour is defined in Java, but as LucTouraille points out its not defined in C++ so you can't expect a particular behaviour which is the same for all compilers.

Another example.

int a = 3;
a = a++ * a++;
System.out.println(a); // will always be 12 in Java.

it is the same as

{
    int t1 = a;
    a = a + 1;
    int t2 = a;
    a = a + 1;
    a = t1 * t2;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

The sequence for the algorithm:

    int a=10;

    a=a++;
    System.out.println(a);

    a=++a;
    System.out.println(a);


    a=(a++);
    System.out.println(a);

Is as follows:

a=++a; means that a is incremented first before assigning the expression to a. Thus a = 11.

a=(a++); means can be understood like

b = a;
a++;
a = b;

Hence why you receiving value as 11.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

They all should work. I'm not sure what language your on. But if your on C/C++ it should work.

student
  • 213
  • 2
  • 5