+ 2
What is undefined behaviour?
let an example .... int x=3; cout <<++x*++x; it gives deferent output in different compiler. .gcc gives 25 turbo gives 20 plz give the reason behind it.
6 Antworten
+ 6
.gcc probably processes both postfixes before doing the computation, whereas turbo does it one at a time.
+ 3
There is the C (or C++) standard, which tells you about how the C language works. The C standard doesn't cover everything though, and so undefined behaviour is born. They do this so writing compilers is easier (and C can run everywhere!).
Your example is undefined due to what's called "sequence point rules" - it's a bit tricky, but Keto already said what's happening and the standard doesn't say which compiler is right. The problem is that you have two "side effects" (adding 1 to x) within the same "sequence".
As of C++11, sequence point rules have been replaced with "sequenced relations". Your example should still be undefined though, even in gcc, for similar reasons. And Turbo is just old of course so it doesn't know C++11.
___
You can find the technical stuff here.
http://en.cppreference.com/w/cpp/language/eval_order
https://en.wikipedia.org/wiki/Sequence_point
+ 1
if I am write in gcc
int x=3;
cout <<++x*++x*++x;
it will gives 150.
plz explain. ..... how it will
+ 1
@Schindlabua: Some behavior is left undefined intentionally so compiler builders can use it to construct better optimizations.
0
in gcc how it will gives 25 plz explain
0
This happens because of different compilers evaluate multiple increment operators differently.