Difficulty: Easy
Correct Answer: It doesn't matters
Explanation:
Introduction / Context:
This C output question tests your understanding of what printf
returns and how assignment within an if
condition behaves. Many beginners assume that an empty printf
“does nothing,” but overlooking its return value leads to incorrect conclusions about the executed branch.
Given Data / Assumptions:
printf
returns the number of characters printed.(ch = printf(""))
assigns the return value of printf
to ch
and uses that value as the condition.
Concept / Approach:printf
returns an int equal to the count of characters written. When given an empty string literal ""
, it prints zero characters and therefore returns 0. The if
statement evaluates the assigned value as a boolean: 0 means false; any nonzero value means true. Because 0 is false, the else
block will execute.
Step-by-Step Solution:
printf("")
→ prints nothing → returns 0.Assignment: ch = 0
.Condition: if (0)
is false → execute the else
branch.Output line: It doesn't matters
followed by a newline.
Verification / Alternative check:
Add a diagnostic: printf("ret=%d\n", printf("")); It will first print nothing, then print ret=0
, confirming the return value of zero for the empty string.
Why Other Options Are Wrong:
printf
, which is not the case for ""
.matters: not printed anywhere in code.No output: the else
branch prints a line.Compilation error: all syntax is valid C.
Common Pitfalls:
Confusing assignment =
with equality ==
; assuming printf
with an empty string returns nonzero; forgetting that the value assigned is exactly what the if
tests.
Final Answer:
It doesn't matters.
float a = 0.7;
, what will this program print and why?
#includefor
loop in C. What does this program print?
#includeswitch
statement in C? Note the statement before any case
label.
#include
Discussion & Comments