+ 2

Why will this 'for' loop loop (albeit just once)?

int x; int main() { for (; x>=0; --x); return 0; } I don't understand the 3rd line. Why will this 'for' loop execute once? The initial value is not even set...

27th Jul 2020, 4:23 AM
Solus
Solus - avatar
4 Answers
+ 6
Solus here "x" is a global variable and all the global variables are by default initialised as 0.
27th Jul 2020, 4:29 AM
Arsenic
Arsenic - avatar
0
Default value of integer is 0
27th Jul 2020, 4:28 AM
Aayush $aini
Aayush $aini - avatar
0
AteFish not necessarily since the only x in scope by the time the for loop is reached,is the global x.In other words,there is no x declared in main's scope
27th Jul 2020, 5:35 PM
Anthony Maina
Anthony Maina - avatar