Simple conditional return in C: given i=45, what does this program print?
#include
int check(int);
int main()
{
int i = 45, c;
c = check(i);
printf("%d
", c);
return 0;
}
int check(int ch)
{
if (ch >= 45)
return 100;
else
return 10;
}
-
A100
-
B10
-
C1
-
D0
-
EUndefined behavior
Answer
Correct Answer: 100
Explanation
Introduction / Context:This checks the basics of conditional branching and return values in C. The function returns one of two constants depending on whether the input meets a threshold.
Given Data / Assumptions:
- Main sets i = 45 and calls check(i).
- check returns 100 if ch >= 45, otherwise 10.
- Printed value is whatever check returns.
Concept / Approach:Direct evaluation of a relational condition determines which return executes. Since 45 meets the greater-than-or-equal condition, the function takes the first branch.
Step-by-Step Solution:Given ch = 45.Evaluate condition ch >= 45 → true.Execute return 100;In main, c receives 100, which is printed.
Verification / Alternative check:Testing with ch = 44 yields 10; with ch = 46 yields 100, confirming the threshold behavior.
Why Other Options Are Wrong:10/1/0 do not match the executed branch. No undefined behavior is present.
Common Pitfalls:Off-by-one misunderstandings around inclusive comparisons (using >= vs >).
Final Answer:100