C Programming - Input / Output - Discussion

4. 

Can we specify a variable filed width in a scanf() format string?

[A]. Yes
[B]. No

Answer: Option B

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.

Nishant said: (Aug 28, 2011)  
Can anybody please explain me the answer.

Pruthvi said: (Oct 30, 2011)  
Answer is YES. We can specify variable field width.

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

// will only take digits chars at run time.

Mini said: (Dec 7, 2011)  
@Pruthvi.

Thank you so much.

Dev said: (Mar 5, 2013)  
What is the correct answer?

Sawant said: (Jun 25, 2014)  
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.

Nazima Khan said: (Sep 4, 2018)  
@All.

Thank you so much.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.