C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Yes / No Questions (Q.No. 4)
4.
Can we specify a variable filed width in a scanf() format string?
Yes
No
Answer: Option
Explanation:
In scanf() a * in a format string after a % sign is used for the suppression of assignment. That is, the current input field is scanned but not stored.
Discussion:
6 comments Page 1 of 1.

Nazima Khan said:   5 years ago
@All.

Thank you so much.

Sawant said:   10 years ago
In scanf(), we can't use variable width specifier, we can only use like:

scanf("%4d",&a);

If you enter 12345678, the scanf simply scan the first 4 digits (ie) 1234 only.

But we can use variable width specifiers in printf():

printf("%*d",4,a);

4 is the width of the integer a.

Dev said:   1 decade ago
What is the correct answer?

Mini said:   1 decade ago
@Pruthvi.

Thank you so much.

Pruthvi said:   1 decade ago
Answer is YES. We can specify variable field width.

scanf("%2d",&a);

// will only take digits chars at run time.

Nishant said:   1 decade ago
Can anybody please explain me the answer.

Post your comments here:

Your comments will be displayed after verification.