What would you like to do?
Can you specify variable field width in a scanf format string If possible how?
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.
Was this answer useful?
Thanks for the feedback!
A gene is a string of triplets that specifies a particular protein.
double would be %e, %f or %g (depending on the format you want), long int would be %ld or %lu (signed or unsigned), long double would be %Le, %Lf or %Lg.
Use '%s', eg: char name; scanf ("%s", name);
cause getchar only read a character and scanf read only word before first space but not other words and letters.
A gene is the string of triplets that specifies a particular protein.
You must specify the variable type and an identifier. int n; // type - int, identifier - n
There are many types of format specifier. Exp:%d (To show the integer) %c(To show the character) %f(Float are digits with decimal points to use it to show them) %s(String to …show the string)
Format specifier is a sequence passed the as the formatting data as by argument
destination (physical/hardware address) Source (physical/hardware address) Start flag (start of message indicator) Recipient sender encapsulated data end of frame …
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.
Pleas refer to the link below.
A char is a single character. A String is a collection of characters. It may be empty (zero characters), have one character, two character, or many characters - even a fai…rly long text. The single quote (') is used to deliniate a character during assignment: char someChar = 'a'; The double quote (") is used to delineate a string during assignment: String someString = new String("hello there"); Note that char is a primitive data type in Java, while String is an Object. You CANNOT directly assign a char to a String (or vice versa). There is, however, a Character object that wraps the char primitive type, and Java allows method calls to be made on the char primitive (automagically converting the char to Character before doing so). i.e. these ALL FAIL: someString = SomeChar; someString = new String(someChar); However, these WILL work: someString = Character.toString(someChar); someString = someChar.toString(); Also note that a String is a static memory allocation, while a character's is dynamic. By that, I mean that when a String is created, it is allocated a memory location exactly big enough to fit the assigned string. Any change to that String forces and entirely new memory location to be allocated, the contents of the old String copied in (with the appropriate changes), and the old String object subject to garbage collection. Thus, making changes to a String object are quite inefficient (if you want that kind of behaviour, use StringBuffer instead). A character is allocated but once; all subsequent changes to a character variable simply overwrite that same memory location, so frequent changes to a character variable incur no real penalty.
the width of the string is would be how thick it is