In C, the if statement needs a conditional expression to decide whether to execute a block of code. This condition is usually a logical or relational expression that returns either true (non-zero) or false (zero).
Syntax of a valid if statement:
if (condition) { // code to execute if condition is true }
What If the Condition Is Missing?
If the conditional expression is missing, the C compiler will throw a syntax error. This is because the if statement needs a valid expression to evaluate.
Example – Compilation Error:
advertisement
#include <stdio.h> int main() { if () // Missing condition { printf("This will cause a compiler error.\n"); } return 0; }
Error Message:
error: expected expression before ')' token
Correct Example with a Condition:
Free 30-Day Python Certification Bootcamp is Live. Join Now!
#include <stdio.h> int main() { if (1) // Condition present, evaluates to true { printf("This will execute because the condition is true.\n"); } return 0; }
You can use any valid expression that evaluates to non-zero (true) or zero (false). Even a constant like 1 (true) or 0 (false) works.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
If you wish to look at all C Tutorials, go to C Tutorials.
advertisement
Related Posts:
- Check C Books
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C Internship