In C programming, what will be the output of the following program? #include<stdio.h> int main() { char ch; if (ch = printf("")) printf("It matters\n"); else printf("It doesn't matters\n"); return 0; }

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:

  • The program compiles with a standard C compiler.
  • printf returns the number of characters printed.
  • The expression (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:

Call: 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:

It matters: would require a nonzero return from 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.

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion