What will be the output of the following C code?
#include <stdio.h>
void main()
{
int i = 0;
if (i == 0)
{
printf("Hello");
break;
}
}
#include <stdio.h>
void main()
{
int i = 0;
if (i == 0)
{
printf("Hello");
break;
}
}A. Hello is printed infinite times
B. Hello
C. Varies
D. Compile time error
Answer: Option D
Solution (By Examveda Team)
The correct answer is D: Compile time errorHere's why:
In C, the `break` statement is used to exit loops (like `for`, `while`, or `do-while`) or `switch` statements.
It's designed to jump out of these control structures.
In this code, `break` is placed directly inside an `if` block, but not within any loop or `switch`.
The C compiler will detect that `break` is used in an invalid context.
Therefore, the program will result in a compile-time error, indicating that the `break` statement is misplaced. The program won't even run.
In summary: The `break` statement is only valid inside loops or `switch` statements.
Join The Discussion
Comments (1)
Related Questions on Control Structures
Which control structure is used to repeatedly execute a block of code in C?
A. for loop
B. if statement
C. switch case
D. while loop
In C, what is the purpose of the 'break' statement within a loop?
A. Continue to the next iteration
B. Exit the program
C. Terminate the loop and exit it
D. Skip the current iteration
What is the purpose of the 'else' statement in C's 'if-else' control structure?
A. Execute the 'if' block
B. Execute the 'else' block
C. Execute both 'if' and 'else' blocks
D. Skip the 'if' block
Which control structure is used to make a decision between two or more alternatives in C?
A. switch case
B. for loop
C. if statement
D. while loop

The break statement is only allowed inside loops (for, while, do-while) or switch statements.
In this code, break; is used inside an if block, but not within any loop or switch, so the compiler will throw an error.