First, the scanf family
1, the scanf family's prototype int scanf (char const *format,...); int fscanf (FILE *stream,char const *format,...); int sscanf (char const *buffer,char const *format,...); Ellipses in each prototype represent a variable-length list of pointers. The values that are converted from the input are stored individually to the memory location where the pointers point. Because the parameter passing of C is a value call, the variable parameter is a list of pointers. Note: If the pointer is not given, it is the variable value. SCANF will use the value of the variable as a pointer, either at the time of the dereference or to cause the program to terminate, or to cause the data in the unpredictable memory location to be overwritten. 2, return value the input stops and returns the number of input values that are converted when the formatted string format reaches the end or the input that is read does not match the type specified by the format string. If the file reaches the tail before any input is converted, it returns EOF. 3. Type matching because scanf is a mechanism that uses variable parameters, functions cannot verify that their pointer parameters are the correct type, so the function assumes they are correct. (so the format character must be consistent with the type that the pointer follows) if the type of the pointer parameter does not match the type of the input data, the resulting value is garbage. Also, neighboring variables can be rewritten. For example: Float a;scanf ("%d", &a); //A is a float data, but uses a shaping pointer to point to variable a. 4. The scanf format code includes the following in the formatting string. Whitespace characters: They match 0 or more whitespace characters in the input and are ignored during processing. Format code (commonly used in%c): They specify how the function interprets the next input character. Other characters: When other characters appear, the next input character must match. If the input character is dropped, if it does not match, the function is no longer read and returned directly. Format code: preceded by%, followed by: an optional asterisk; an optional qualifier; format Codes (1) Asterisks: Converted values are discarded without storage, skipping input characters that are not required. (2) Width: Limit the number of input characters that are read for conversion. If the width is not given, the function reads characters until a white space character is encountered. (3) qualifier: Modifies the meaning of some format codes. Note Convert allA short, long, double, long double should be added qualifier. If not added will result in a longer variable only part of the initialization, a shorter variable of the neighboring variable is also modified. These depend on the length of the type in the machine.
Format Code \ Qualifier |
H |
L |
L |
D,i,n |
Short |
Long |
|
O,u,x |
unsigned short |
unsigned long |
|
E,f,g |
|
Double |
Long double |
For example: Short var_a;scanf ("%hd", &var_a); (4) Format code: a single character that represents how the input character is interpreted, and the pointer-to-list pointer type.
|
|
|
c |
char * |
|
i D |
int * |
signed integers are converted. %d is interpreted as decimal. %i determines the cardinality of a value based on the first character, and the representation of an integer character-value constant. 10,034,0xa2 |
u o x |
unsigned * |
no character Integer number is converted. U: Decimal, O: octal, x: Hex |
e F G |
float * |
look for a floating-point value. His form must be like a floating-point literal constant, but the decimal point does not have to be |
s |
char * |
read Takes a string of non-whitespace characters, and the input stops when a blank is found. The back automatically adds Nul. You must ensure that the array space is large enough |
n |
int * |
number of processing characters |
5, using scanf to achieve line-oriented input. It is difficult to use scanf to keep the line boundary synchronized because SCANF uses the carriage return as a blank character processing. In order to implement row orientation. Can be paired with Fgets. First read a row with fgets, and then use the sscanf to read the row processing. 6. Use SSCANF to handle variable format input. int a,b,c;
Fgets (Buf,20,stdin);
Char *p = STRRCHR (buf, ' \ n ');
*p = ' + '; Remove carriage return character
if (sscanf (buf, "%d%d%d", &a,&b,&c)! = 3)
{
A = 1; Defalut value of a
if (sscanf (buf, "%d%d", &b,&c)! = 2)
{
b = 1; Default value of B
if (sscanf (buf, "%d", &c)!=1)
{
printf ("Input error\n");
Exit (1);
}
}
}
printf ("A =%d\nb=%d\nc=%d\n", a,b,c);
Second, the printf family 1, the prototype int printf (char const *format,...); int fprintf (FILE *stream,char const *format,...); int sprintf (char *buffer,char const *FORMAT,...) ; 2, the type matches the printf function and scanf, you cannot verify whether a value has the correct type represented by the format code. So it is the programmer's responsibility to ensure that they match each other. 3. printf format Code The format string contains the formatting code, which causes the next value of the argument list to be formatted as specified, and for other characters to be output as-is. The format code is preceded by a% and can be followed by: Flag character, field width, precision, modifier, #标志, Format code (1) flag character:
Sign |
Meaning |
- |
Left-aligned, default right-aligned |
0 |
When right-aligned, use 0 to fill unused columns to the left; |
+ |
When a number is positive, precede with a + sign, which is not displayed by default |
Space |
When a number is positive, preceded by a space, the default does not show |
(2) Field width: Specifies the minimum number of characters for the output, if the output is less than the field width. Modify the output (3) precision according to the flag character: Action on%s: Specifies the maximum number of characters to be converted action on%f: Specifies the number of digits (4) modifiers that appear after the decimal point:
Modifier |
Action Object |
Representation type |
H |
D,i,o,u,x |
Short type integer |
L |
D,i,o,u,x |
Long-integer |
L |
E,f,g |
Long double type data |
(5) Format code
code |
parameter |
|
c |
int |
|
d I |
int |
|
TD valign= "Top" >o u x
unsigned int |
|
e, F, G |
double |
|
s |
char * |
print a string | /tr>
n |
int * |
number of characters to print |
/table>
(6) #标志 # flag can be used for format code: O,X,E,F,G is the unsigned number and floating point o: The resulting value starts with 0; x: starts with 0x; (these two are useful.) E,f,g: Ensure that the result always contains a decimal point even if there is no number behind it. Long Double A = 3.14;printf ("a =%08.3LF", a); Right-aligned, beginning with 0, character width 8 bits, precision 3 bits, output as long double. Thirdly, using sprintf and scanf to realize the conversion of string and numeric value sscanf can realize the conversion of string to numeric value, and use sprintf to realize the conversion of numeric value to string #include<stdio.h>
int main ()
{
float a = 3.14;
Double b; Char buf[20]; SSCANF ("3.42", "%lf", &b); Converts a string to a numeric value double
sprintf (buf, "%.2f", a); Converts a double value to a string
printf ("B =%.2f\n", b);
printf ("Buf is:%s\n", buf);
Other functions provided by the standard library for string conversion to Integer/float are: int atoi (char const *string), long int atol (char const *string), long int strtol (char const *ST Ring,char **unused,int base); long int strtoul (char const *STRING,CHAR **unused,int base); Double atof (char const *string);d ouble strtod (char const *string,char **unused); Precautions: 1, skip leading whitespace characters, ignore illegal suffix 2, for integral type, When Base=0, the string's binary is determined by the literal string. (Octal (starting with 0), decimal (default), hexadecimal (starting with 0x)), 3, if cannot be converted to the corresponding type, returns 04, unused pointer to a pointer to a character that cannot be converted. Iv. the number of digits obtained by sprintf for an integer usually for an integer data, we need to obtain its number of bits by dividing it with 10, 100, 1000, and so on to determine the total number of bits. The following program provides a good way to get the data bit number: int number,data;
data = 12345;
Char buf[20];
sprintf (buf, "%d%n", Data,&number); Use snprintf to prevent access to memory out of bounds: snprintf (buf,20, "%d%n", Data,&number);
printf ("Data is%d,has%d characters\n", Data,number), uses printf's%n format to record the number of printed characters to count the number of bits of data.
scanf and printf (go)