+ 5

What would be the output of following code?

#include<iostream.h> void main() { int a; a=1; cout<<++a+++a; }

11th Aug 2017, 2:12 PM
Mohit Sharma
Mohit Sharma - avatar
8 ответов
+ 9
one more thing void main is not valid c++ either. it can be used in some compilers but others will most certainly complain about it and not compile. int main() is the standard😊
11th Aug 2017, 2:43 PM
jay
jay - avatar
+ 5
conio.h could not be found clrscr is not defined but it could be 6. We should address the concerns here I guess. Turbo c++ is very very obsolete. Please upgrade to something that supports 1998 standard c++ or higher. Using multiple pre/post fix increments in a single statement results in undefined behaviour and is considered bad practise. Please do not do this.
11th Aug 2017, 2:13 PM
jay
jay - avatar
+ 5
It will not be an error but some compilers will handle it differently than others. so the result maybe 5 on some compilers and 6 on others But here is how it may work remembering that prefixes are evaluated before the expression is: a=1 a+1 (++a) a = 2 a+1 (++a) a = 3 a + a ( 3 + 3)
11th Aug 2017, 2:39 PM
jay
jay - avatar
+ 3
Compilation terminated
11th Aug 2017, 2:16 PM
Iwan
Iwan - avatar
+ 2
Thanks Jay
11th Aug 2017, 2:31 PM
Mohit Sharma
Mohit Sharma - avatar
+ 2
What if we write this code in code blocks removing conio.h and clrscr.. then what would be the result Would it return error due to undefined behaviour (And the answer in C++ was coming 6.. If anyone know how it is compiled then plz reply.)
11th Aug 2017, 2:35 PM
Mohit Sharma
Mohit Sharma - avatar
+ 2
OK Thanks for the help😊👍
11th Aug 2017, 2:40 PM
Mohit Sharma
Mohit Sharma - avatar