LearnFrenzy
Sign in Take a free test

C Input / Output · Predict the Output · Question 9

scanf with field widths %2d and %3s – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following C program when the input is 20261111abcdef⏎?

main.c
#include <stdio.h>

int main(void) {
    int a, b;
    char s[4];
    scanf("%2d%3d%3s", &a, &b, s);
    printf("%d %d %s\n", a, b, s);
    return 0;
}
  • A 20 261 111 Correct
  • B 2026 1111 abc
  • C 20 261 abc
  • D 20 26 111
Correct Answer: Option A

A width limits how many characters a conversion consumes: %2d takes "20", %3d takes "261", and %3s takes the next three non-space characters, "111".

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