LearnFrenzy
Sign in Take a free test

C Input / Output · Predict the Output · Question 7

sscanf return value when the second field does not match Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = -1, b = -1;
    int r = sscanf("12 apples", "%d %d", &a, &b);
    printf("%d %d %d\n", r, a, b);
    return 0;
}
  • A 1 12 -1 Correct
  • B 2 12 0
  • C 0 -1 -1
  • D 1 12 0
Correct Answer: Option A

The first %d stores 12; the second fails at "apples", so scanning stops. One item was assigned, so the result is 1 and b keeps its old value.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links