What is undefined behaviour? | Sololearn: Learn to code for FREE!
+ 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.

16th Nov 2016, 7:57 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
6 ответов
+ 6
.gcc probably processes both postfixes before doing the computation, whereas turbo does it one at a time.
16th Nov 2016, 8:09 PM
Keto Z
Keto Z - avatar
+ 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
16th Nov 2016, 8:33 PM
Schindlabua - avatar
+ 1
if I am write in gcc int x=3; cout <<++x*++x*++x; it will gives 150. plz explain. ..... how it will
16th Nov 2016, 8:41 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
+ 1
@Schindlabua: Some behavior is left undefined intentionally so compiler builders can use it to construct better optimizations.
16th Nov 2016, 9:06 PM
Stefan - avatar
in gcc how it will gives 25 plz explain
16th Nov 2016, 8:35 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
This happens because of different compilers evaluate multiple increment operators differently.
21st Nov 2016, 9:16 AM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar