#include <iostream> using namespace std; int main() { int a=1 ,b; a=b; cout << b+a; return 0; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

#include <iostream> using namespace std; int main() { int a=1 ,b; a=b; cout << b+a; return 0; }

Why this code outputs 16

25th Jan 2019, 6:42 AM
Akash Kumar
Akash Kumar - avatar
8 Answers
+ 1
b has an undefined value that happens to be 8.
25th Jan 2019, 10:39 AM
Zen
Zen - avatar
+ 6
Zen is there any specific reason why it has value of 8 only? I tried playing with the code a bit but everytime the undefined var has value of 8 meaning it isn't random 🤔
25th Jan 2019, 1:44 PM
Frost
Frost - avatar
+ 2
First you assign 1 to a Then you make b equal to a so b now is 1 cout << b+a prints the sum of a and b => a=1; b=1 ==> a+b =2
25th Jan 2019, 9:32 AM
Etabeta1🇮🇹
Etabeta1🇮🇹 - avatar
+ 1
//नमस्ते #include <iostream> using namespace std; int main() { int a=1 ,b; b=a; cout << b+a; return 0; } //this code outputs 2
25th Jan 2019, 6:43 AM
Akash Kumar
Akash Kumar - avatar
+ 1
You haven't initialized b in your code.
25th Jan 2019, 10:34 AM
Zen
Zen - avatar
+ 1
PLease explain above code whose output is 16
25th Jan 2019, 10:35 AM
Akash Kumar
Akash Kumar - avatar
+ 1
Yes i am also thinking the same But as i did not completed everything in c++ ,i hope to understand it latter
25th Jan 2019, 1:46 PM
Akash Kumar
Akash Kumar - avatar
0
Yes I did not initialised b, but how the output fetch 16??
25th Jan 2019, 10:37 AM
Akash Kumar
Akash Kumar - avatar