कोड के निम्नलिखित भाग का आउटपुट क्या होगा?
What will be the output of the following piece of code?
#include <stdio.h>
int main() {
for(i = 0;i < 8; i++);
printf("%d", i);
return 0;
}
A
0
B
1234567
C
8
D
infinite loop
Explanation
Let's break down the given code:
Code:
#include
int main() {
for(i = 0; i < 8; i++);
printf("%d", i);
return 0;
}
Explanation:
-
The
forloop:
Theforloop has a semicolon (;) after it, which means the loop doesn't contain any body (it's an empty loop). It simply incrementsifrom 0 to 7. -
After the loop completes,
iwill be 8, because the loop condition (i < 8) fails whenireaches 8. -
The
printf("%d", i);statement:
This prints the value ofi, which is 8 after the loop terminates.
Output:
So, the output of this code will be:
(C) 8.
Correct Answer: C) 8