What would you like to do?

Can you specify variable field width in a scanf format string If possible how?

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

You can't specify a variable field with a fixed format string, but you can get around this by making the format string variable: int width; char format[20]; /* or whatever size is appropriate */ int value; ... sprintf(format, "%%%dd", width); /* generates a string like "%5d" */ scanf(format, &value); The only drawback to this method, other than requiring two statements, is that the compiler can't do a sanity check on the arguments to scanf like it can when the format is a string constant. If you want to specify a variable width in a printf format string (as opposed to scanf), you can do the following: printf("%*d", width, num); That will use the value of "width" as the width for formatting the value of "num" as a decimal integer.
50 people found this useful
Thanks for the feedback!

Can you specify variable field width in scanf format string?

Yes, you can specify variable width, however you can't specify precision. The format tags are as follows: . scanf: %[*][width][length]type . printf: %[*][width][.precision][

What is string variable?

A string variable is a programming language construct that holds text. For example, the text "The sky is blue" could be stored to a string variable, then later in the program,

What is a string variable?

an alphanumeric or string variable is used to represent any non numeric value. it can be represented by one or more letters or digits but it should start with a letter and end

How can we replace the value of const variable using scanf statement?

You cannot change the value of a const variable. That's what const means - it is constant. If you are considering "trickery" using pointers and the scanf function, understand

Why not you simply put the decimal integer into the original string as compared to the format specifier?

You can certainly do that ... printf ("This is a number: 12345\\n"); ... but that does not have the same value as placing the value in a variable and converting the variab

What is a format specifier?

A format specifier is a programming code used by a computer  programming language to control how input or output data will be  formatted to make it easier for humans to read

What are the limitations of getchar and scanf functions for reading strings?

The scanf() function is used to read formatted data from standardinput. Whitespace is ignored thus it cannot be used to read astring containing spaces. The getchar() funct