Why the output is 19? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 7

Why the output is 19?

int main() { int i=4,x; x=++i + ++i + ++i; cout<<x; return 0; }

21st Feb 2017, 7:32 AM
Aman gautam
Aman gautam - avatar
11 Respostas
+ 8
No one can tell in case of c++. Different compilers implement multiple pre-increment and post-increment in one statement in different ways. There is no clear rule how to evaluate multiple increment statements in a single executable line in c++. Try 10 different compilers and you will get at least 3-5 different outputs. Java is clear in this case and answer is always 18 for this question.
21st Feb 2017, 8:32 AM
Megatron
Megatron - avatar
+ 3
I think when ++i I=5 ++i I=6 then I+I = 12 then ++i I=7 then I+I+I = 12+7 =19 is this right
21st Feb 2017, 8:38 AM
Aman gautam
Aman gautam - avatar
+ 2
x = ++I(i = 5) + ++i (i = 6) + ++i(i = 7) x = 5 + 6 + 7 x = 18
21st Feb 2017, 8:26 AM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
@tofik how??
22nd Feb 2017, 1:54 AM
Aman gautam
Aman gautam - avatar
+ 2
initially i=4, then we add ++i(5) to it 3 times, so 4 + 5*3=19
17th Apr 2017, 5:44 AM
deepak mangla
deepak mangla - avatar
0
I don't know. Maybe x was unset? But it is overwritten anyway. So that's not it. In Java this outputs 18: public static void main(String[] args) { int i=4,x; x=++i + ++i + ++i; System.out.println(x); } As it is expected for me to sum the 5+6+7.
21st Feb 2017, 8:26 AM
mgis
mgis - avatar
0
yes the output is 18
21st Feb 2017, 9:03 AM
akshay
akshay - avatar
0
4+5+6
24th Feb 2017, 2:09 PM
Sabeek Ahamed S
Sabeek Ahamed S - avatar
0
13
25th Feb 2017, 7:07 AM
SalmanMusthafa
0
because is grater than input
25th Feb 2017, 5:25 PM
Joshu
Joshu - avatar
- 1
var x = 9 + 10 ;
21st Feb 2017, 9:29 PM
tofik