answersLogoWhite

0


Best Answer
Answer

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.

Answer

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.

User Avatar

Wiki User

โˆ™ 2009-10-13 07:14:11
This answer is:
User Avatar
Study guides

What is a programming language

What does DOS stand for

What is a software that is distributed for free

What is application software

โžก๏ธ
See all cards
3.79
โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…
19 Reviews

Add your answer:

Earn +20 pts
Q: Can you specify variable field width in a scanf format string If possible how?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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

ftghdd ftghdd ftghdd ftghdd


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]typeprintf: %[*][width][.precision][length]typeAs you can see, scanf lacks the precision tag but does include the length tag.See: http://msdn2.microsoft.com/en-us/library/kwwtf9ch.aspx


How do you create a variable in javascript?

In most programming languages you have to specify the variable-type before you declare the variable, (String, int, double). But in javascript you just have to type var..Example:var variable1 = "Hello world"; String variablevar variable2 = 123; int variable


What are the different types of format specifiers in c?

format specifier also called as control specifier or variable formatters. format string also called arguments.


How the printf function uses the format string to set the width and presicion of an output variable?

%<width>.<precision>f


What do mean by format specifier?

A format specifier is a sequence of characters starting with a percent sign (%) and ending with a code in the format string for the various calls to the printf and scanf class of run-time library calls. These sequences are replaced with the interpretation of the next variable into (printf) the output string, or from (scanf) the input string.


What is a variable declaration?

Variable declaration is when you declare a variable. For example: String foo; The data type is String and now I can modify foo and don't need to type String again. It can be an instance variable or a local variable.


Purpose of printf?

"printf" stands for "print formatted", and its purpose is to output a list of variables given a format string. This format string can specify padding and alignment of strings, the number of digits before and after decimal places in floating point numbers, and so on, thus allowing the programmer to output data in a precise format.


What is the code required to convert an integer variable to a string variable in Java?

There are several different methods to convert an integer variable to a string variable in Java. For example, one can use the following code to convert an integer variable to a string variable: Integer.toString(number)


Why percent 3d is used in c coding?

The string %3d in a format specification string means to convert the associated variable into a decimal with at least three digits of space allocated in the result buffer.


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 variable into a string ... int i = 12345; printf ("This is a number: %d\n", i); That's the whole point of format specifiers - to initiate a conversion from one place to another.


Why not 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 variable into a string ... int i = 12345; printf ("This is a number: %d\n", i); That's the whole point of format specifiers - to initiate a conversion from one place to another.

People also asked