Top Answer
User Avatar
Wiki User
Answered 2009-10-13 07:14:11

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.

User Avatar

Your Answer

Still Have Questions?

Related Questions

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.

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.

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.

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.

How do you give values to variable?

The format is [access types] [variable type] [variable name] = [value] For example: int i = 10; // Here there is no access type private static String str = "Hello, World!"; // Private and static access types.

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, that text could be displayed.

Can an int value be assigned to String variable?

In C when assigning an integer variable to String variable a compilation error occurs as Incompatible Type Conversion.So that integers cannot get assigned to String variables.

How can you check if a variable is a string or an integer in PHP?

Integers - The "is_int()" function can be used to check if a variable is has an integer for its value. ---- is_int($variable); // Returns true if $variable is an integer - otherwise false ---- Numeric Strings - Numeric strings are strings with numbers (or things that count as numbers) in them. Numeric-string variables are not integer variables. Numeric-string variables are passed on through forms, instead of integer variables - if you were wondering. Check form values using string formats, and not integer formats. The "is_numeric()" function can be used to check if a variable is a string with numbers - and only numbers - in it (except things that add up to be numbers). ---- is_numeric($variable); // Returns true if $variable is a string, and only contains numbers (broadly speaking) - false otherwise ---- Strings - String values are just text, basically. String variables can contain integers, but that does not make it an integer-type variable - it makes it a numeric string variable. The "is_string" function can be used to check if a variable contains the value of a string. ---- is_string($variable); // Returns true if $variable is a string - false otherwise

What is string in c plus plus?

A String is a variable that represents a list, or array, of characters. They are normally assigned using double quotes (""). When using strings you must import string header file#include and then an example of a stringstring mystring = "This is a string";This give the variable 'mystring' a value of "This is a string".A string can also be referred to as an array of characterschar string [10];Would make the variable string to an array of characters with length ten.

What is the purpose of scanf function?

The scanf() function reads formatted input from stdin according to a format string and assigns the data to specified variable(s). The input data is a character sequence which is converted to the appropriate type, as per the format string, before being assigned to the corresponding variable. Examples: unsigned x; scanf("%u", &x); In the above example, we read an unsigned integer (%u) from the stdin character stream and assign the resultant value to the variable x.

Is the value 3 a string variable?

No, it is an integer. You can save an integer value to a string variable but, in this case the value is explicitly stated to be 3.

How do you convert string value into integer using javascript?

There's a number of ways you can do it. variable = variable * 1 multiplies the variable by one, and converts the variable to a number if it's a string. Because * 1 doesn't change the value, this is one way of converting a string into an integer. Another is using JavaScript's parseInt(variable).

What are the components of print f and scan f?

printf: format string + value list scanf: format string + address list

Is madam string or variable?

No because madam is a string and character as the same time

In PHP you can use both single quotes ( ' ' ) and double quotes ( ) for strings?

You can use single quotes $variable = 'string'; You can use double quotes $variable = "string"; You can use the Heredoc method $variable = <<<EOT Very Long String EOT; And you can use the Nowdoc method (after PHP 5.3.0) $variable = <<<'EOT' Very Long String EOT;

What is the meaning of String args in java?

it means that we are declaring a variable args which have String datatype...

How do you fix a runtime error 91?

You can fix runtime error 91 if you verify the given code of the program because the error code indicates that the object variable name of the given programs doesn't match. As we know that if the variable is integer then we must pass the integer value and if the variable is string then we pass string values because if the values format is missing then the program hangs or terminates with the error code runtime error 91 object variable don't match.

What is difference between zero value and NULL strings?

AnswerProgramming languages store data in different data types. A zero value would belong to a numeric data type such as float, single, double, integer, long, etc. A null string is a variable that references a string (sequence of characters) that is not yet pointing to anything.AnswerA null string can also be a string of zero length.AnswerSeveral programming languages including C and C++ and several assembly languages store strings in "ASCIZ" format, also called "C string" format.In those languages, a string variable contains only a pointer to the first character in the string, and the end-of-string is marked with the ASCII NUL character.A programmer using strings in "ASCIZ" format must be aware of the two different ways that a string variable may store no characters:One way a ASCIZ variable may store no characters:A string variable may contain the null pointer constant, which is represented by the constant integer 0. All such "null pointers" point to the same location, represented by the constant integer 0, and often called NULL.No valid string is ever located at that location.For example,char * winner_name = 0;or equivalentlychar * winner_name = NULL;Typically a programmer must write explicit code to check for this possibility first.For example,if( winner_name ){ cout The other way a ASCIZ variable may store no characters:The string variable may contain the address of a valid memory location that contains no other characters before the end-of-string NUL character.Some programs have many such locations in memory, located at a variety of addresses (that address is stored in the string variable), that contain such a "null string" or "zero-length string".For example,char buffer[BUFFER_SIZE] = "";or equivalentlychar buffer[BUFFER_SIZE] = {'\0'};.Sometimes programmers can write the code that handles normal strings in such a way that that code also handles zero-length strings, with no explicit check for a "zero-length string" possibility.

Still have questions?

Trending Questions
How to Make Money Online? Asked By Wiki User
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Unanswered Questions