The program outputs 1 because the for loop terminates when i becomes 0. The for loop has no initialization, condition, or increment expressions, so it will run indefinitely unless a break statement is executed. The loop body consists of a single if statement that checks if i is non-zero,and if so, breaks out of the loop. Otherwise, i is divided by 2 and assigned back to itself. Since i is an integer, the division will truncate any fractional part. Therefore, the loop will iterate until i becomes 0, which will happen after one iteration, as 1 / 2 = 0. The printf function then prints the value of i as a deci-mal integer using the %d format specifier.
References = CLA – C Certified Associate Programmer Certification, [C Essentials 2 - (Intermediate)], C For Loop, C If…Else Statement
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit