What will be the output of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; ) { printf("%d ", i); i++; } return 0;}
Question
What will be the output of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; ) { printf("%d ", i); i++; } return 0;}
Solution
The output of the code will be:
0 1 2 3 4
Explanation:
The code is written in C language. It initializes an integer 'i' to 0. Then it enters a for loop where it doesn't have an initialization or increment/decrement part inside the loop syntax. Instead, the increment part is written inside the loop body.
The loop will continue as long as 'i' is less than 5. In each iteration of the loop, it first prints the value of 'i' followed by a space, and then increments 'i' by 1.
So, in the first iteration, 'i' is 0. It prints 0 and then increments 'i' to 1.
In the second iteration, 'i' is 1. It prints 1 and then increments 'i' to 2.
This process continues until 'i' is 4. After printing 4, 'i' is incremented to 5. Now, 'i' is not less than 5, so the loop terminates.
Hence, the output of the code is the sequence of numbers from 0 to 4, each followed by a space.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.