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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

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][length]type


As you can see, scanf lacks the precision tag but does include the length tag.

See: http://msdn2.microsoft.com/en-us/library/kwwtf9ch.aspx

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

ftghdd ftghdd ftghdd ftghdd

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you specify variable field width in scanf format string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you call a function given its name as a string?

we have to construct a table of two field structures,where the first field is the funtion name as a string and the second field is just the function name.then search the table to get a string match in the first field and use the second field to call the function.


What does Error converting data type to numeric means?

You must have declared a method which tries to convert a variable/field value into a numeric value but that is not possible. Refer to the line on which the error is found and remove the method which tries to convert the variable/field. Note: Java allows type casting so a string cannot be converted into integer even if a method tries to change the value


How do you sort string values by field calculator in ArcMap?

For example, there are few string values of "1-5", ">5", "0-0.9", and "<0" from one field in attribute table. Then I need to sort both "1-5" and ">5" to a new field. How do I make the field calculator for this? Thank you!


What is a field in java?

A field is an attribute. A field may be a class's variable, an object's variable, an object's method's variable, or a parameter of a function. class bike{ static int bikes;int gear;int cadence; void create( int newGear, int newCadence ){bikes = bikes + 1;gear = newGear; cadence = newCadence;} int getSpeed(){int speed = gear*cadence*5*3.141;return speed;}} 'bikes' is a class's variable (class variable) (static field). 'gear' and 'cadence' could be an object's variables (instance variables) (non-static fields). 'speed' is an object's method's variable (local variable). 'newGear' and 'newCadence' are parameters of a function (parameters). Refer to related links section


Relationship between flux density and field strength?

In simple terms, if flux density increases, then field strength increases and vice versa. The flux density is equivalent to field strength times with a variable.

Related questions

In IPv6 each address contains an or a variable-length field at the beginning of the address that indicates what type of address it is?

format prefix


What is variable field length?

The amount of data stored in such fields can vary in length. Usually you do specify a maxmimum length so that databases can optimize how data is stored and accessed.


How can you call a function given its name as a string?

we have to construct a table of two field structures,where the first field is the funtion name as a string and the second field is just the function name.then search the table to get a string match in the first field and use the second field to call the function.


What characteristics in a database fields may have such as data type field name field size and field format?

What characteristics could a database field have, such as data type, field name, field size and field format?


How do you validate empty fields in PHP?

PHP provides a function called empty($var), which tests if $var is "empty." Empty has an odd definition, since the function returns true if the variable loosely equates to False. Although a zero-length string would be considered empty, so would the string "0" (among other irrelevant values).To accept the value "0" from a field, use strlen($var). It tests the length of any variable, after casting it to a string. If this function truly equals integer zero (compared with triple-equals), the variable must be empty.


In craps which of these numbers is NOT in the field?

You didn't specify which numbers. The field numbers are 2,3,4,9,10,11,12. The numbers that are not in the field are 5,6,7,8


What does Error converting data type to numeric means?

You must have declared a method which tries to convert a variable/field value into a numeric value but that is not possible. Refer to the line on which the error is found and remove the method which tries to convert the variable/field. Note: Java allows type casting so a string cannot be converted into integer even if a method tries to change the value


What is instance variable in java?

That refers to a variable attached to an object - also known as a field.


What is the unique name called for a data field?

String


When designing a table why is it important to place field properties within each field?

Fields specify how a field can work, what kinds of data can be put in to a field size and other things. You need to specify formats for numbers and dates. You need to specify the size of text fields. There are many optional properties which can prove useful in different situations. Doing validation on a field can be useful to reduce errors. You can ensure data is entered into a field. There are so many other things that properties do. So they are an essential part of designing a field in a database.


How do you plow?

Tie the string to the ox and make it run in the field.


Who is the field marshal of air force at present?

Please specify which country you are referring to.