During the written test today, I encountered a question about C-language scanf functions.
int x;float y;scanf("%3d%f",&x,&y);// input 123456 678 enter and then what's the value of x and yprintf("%d %f",x,y);
The execution result of the program is:
123 456.0000000
It seems that I am not very familiar with the scanf function. Now let's look at the description of the scanf function as follows:
Format: String sequence, which controls how the characters extracted from the stream are processed.
Blank characters: The function reads and ignores any blank characters (including spaces, line breaks, tabs, and other characters) before the next non-blank character ). Single
The white space character refers to any number of white spaces extracted from the stream.
Non-blank characters, except for the format specified (%): Any part that is neither a blank character nor a part of the format specified character (starting with %) will cause the function to read the next character in the stream,
The function compares the non-blank character with the character in the stream. If the character is equal, it discards the character and reads the next character.
The substring is not read.
Format specifier: a sequence formed by an initial percent sign (%) represents a format specifier, which is used to specify the returned from the stream and stored in
The type and format of the data in the address.
% [*] [Width] [length] specifier
Width: specifies the maximum number of characters that can be read by the current read operation.
Length: change the type of the storage area to which the additional parameter points. You can only select one of HH, H, l, ll, J, Z, T, and L.
In the printf function, the formula is slightly different, as shown below:
% [Flags] [width] [. Precision] [length] specifier
Flags:-, +, #, 0, where '-' is used for left alignment. The default value is right alignment. '-' is used to add positive and negative signs to a number. '#' is used for % # O, % # X, % # X. In this way, prefix o15, 0x15 is added to the printed octal and hexadecimal values;
'0' indicates that 0 is used to replace spaces in alignment.
Width: specify the minimum width when printing a character. If the width is not enough, use a space instead. width can also be replaced by '*'. In this case, an additional parameter needs to be used in the additional parameter to specify *
Table Array, for example:
Printf ("% * D % * d \ n", 7,5, 3,4 );
Printf ("% 7D % 3d \ n", 5, 4 );
The execution results of the two lines of code are identical.
. Precision: number of digits after the decimal point. If the number is insufficient, use 0 instead. You can also use '*' to replace a specific number, but you must specify the number '*' in the additional parameter.
Length: similar to the length in scanf, it is used to modify the specifier after % and change the data type.
Scanf, printf function details