Why this code outputs 1 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code outputs 1 ?

#include <iostream> using namespace std; int main() { int n = 0 ; int a[] = {3, 4} ; n++ ; a [n] = n ; cout << a[1] ; return 0; }

22nd Jan 2017, 9:59 PM
Amr Monsef
Amr Monsef - avatar
2 Answers
0
because a[n] = n means a[1] = 1
22nd Jan 2017, 10:10 PM
Dean Marinov
Dean Marinov - avatar
0
You are setting the object gotten in the a[n] index the " n " value that after "n++" becomes 1. So the machine understand this : a[1] = 1 Finally you print out " a[1] " that after the setting is 1
22nd Jan 2017, 10:15 PM
Gian Carlos Juarez Velasquez
Gian Carlos Juarez Velasquez - avatar