What would you like to do?
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; /* 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
Was this answer useful?
Thanks for the feedback!
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
Answer ftghdd Answer ftghdd Answer ftghdd Answer ftghdd
The technique is known as algebra.
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.
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… with a dollar sign some examples are a$,r45$ etc....
Pleas refer to the link below.
How the printf function uses the format string to set the width and presicion of an output variable?
cause getchar only read a character and scanf read only word before first space but not other words and letters.
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… that this is not supported, is highly non portable, and may fail, depending on whether or not the implementation places its const data in read only memory. Besides, most modern compilers will not allow you to place a const variable as a non-const argument to a function. Use the language within its defined boundaries.
I guess you wanted to ask, why is it scanf ("%s", array) and not scanf ("%s", &array). Well, array is by definition a pointer to the first element: array = &array …
Certainly. That's what sequence %s is good for.
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…le 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.
Use '%s', eg: char name; scanf ("%s", name);
Because the name of the string (array) is sufficient to pass the address of the first element. By the way - it is not safe to pass strings to scanf() without doing bounds che…cking.
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… or write. Different computer programming languages use different syntax for format specifiers, some (e.g. FORTRAN's FORMAT statement) of which are quite cryptic and confusing, others are easier to understand and code.
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…ion is used to read a single characterfrom standard input and is functionally equivalent to getc(stdin) . Although they can both be used to read stringsfrom standard input, reading a string one character at a time isinefficient. To read a string from standard input we can either use fgets() or (since 2010) getline() .