In C, what does scanf return when it successfully reads one decimal integer? Consider the following minimal program and assume the user enters 25. #include <stdio.h> int main() { int i; printf("%d ", scanf("%d", &i)); return 0; }

Difficulty: Easy

Correct Answer: 1

Explanation:


Introduction / Context:
This question is about scanf's return value. scanf returns the number of input items successfully matched and assigned, not the values read, and not the number of characters consumed.


Given Data / Assumptions:

  • Format string is "%d".
  • The input provided is 25.
  • i is a valid pointer target of type int.


Concept / Approach:
When scanf matches one %d and stores it into i, it returns 1 to indicate one item successfully assigned. If input is malformed or EOF occurs before assignment, it would return 0 or EOF respectively.


Step-by-Step Solution:
scanf tries to match a single integer token."25" matches → conversion succeeds → i receives 25.Return value becomes 1; printf prints 1.


Verification / Alternative check:
Entering non-numeric text such as "hi" would cause scanf to return 0. Piping EOF immediately typically yields EOF (often -1) depending on library.


Why Other Options Are Wrong:
"25" is the value assigned, not the return count. "2" and "5" are unrelated to scanf's return semantics here.


Common Pitfalls:
Assuming scanf returns the parsed value rather than the count of successful conversions.


Final Answer:
1

More Questions from Input / Output

Discussion & Comments

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