标签:www. bsp stdin tput mat count floating 说明 arch
int scanf ( const char * format, ... );
format
specifier | Description | Characters extracted |
---|---|---|
i | Integer | Any number of digits, optionally preceded by a sign (+ or -). Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f). Signed argument. |
d or u | Decimal integer | Any number of decimal digits (0-9), optionally preceded by a sign (+ or -). d is for a signed argument, and u for an unsigned. |
o | Octal integer | Any number of octal digits (0-7), optionally preceded by a sign (+ or -). Unsigned argument. |
x | Hexadecimal integer | Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -). Unsigned argument. |
f, e, g | Floating point number | A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod). Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X . |
a | ||
c | Character | The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. |
s | String of characters | Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence. |
p | Pointer address | A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf. |
[characters] | Scanset | Any number of the characters specified between the brackets. A dash (-) that is not the first character may produce non-portable behavior in some library implementations. |
[^characters] | Negated scanset | Any number of characters none of them specified as characters between the brackets. |
n | Count | No input is consumed. The number of characters read so far from stdin is stored in the pointed location. |
% | % | A % followed by another % matches a single %. |
sub-specifier | description |
---|---|
* | An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument). |
width | Specifies the maximum number of characters to be read in the current reading operation (optional). |
length | One of hh, h, l, ll, j, z, t, L (optional). This alters the expected type of the storage pointed by the corresponding argument (see below). |
specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x | f e g a | c s [] [^] | p | n | |
(none) | int* | unsigned int* | float* | char* | void** | int* | |
hh | signed char* | unsigned char* | signed char* | ||||
h | short int* | unsigned short int* | short int* | ||||
l | long int* | unsigned long int* | double* | wchar_t* | long int* | ||
ll | long long int* | unsigned long long int* | long long int* | ||||
j | intmax_t* | uintmax_t* | intmax_t* | ||||
z | size_t* | size_t* | size_t* | ||||
t | ptrdiff_t* | ptrdiff_t* | ptrdiff_t* | ||||
L | long double* |
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.
If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.
int printf ( const char * format, ... );
specifier | Output | Example |
---|---|---|
d or i | Signed decimal integer | 392 |
u | Unsigned decimal integer | 7235 |
o | Unsigned octal | 610 |
x | Unsigned hexadecimal integer | 7fa |
X | Unsigned hexadecimal integer (uppercase) | 7FA |
f | Decimal floating point, lowercase | 392.65 |
F | Decimal floating point, uppercase | 392.65 |
e | Scientific notation (mantissa/exponent), lowercase | 3.9265e+2 |
E | Scientific notation (mantissa/exponent), uppercase | 3.9265E+2 |
g | Use the shortest representation: %e or %f | 392.65 |
G | Use the shortest representation: %E or %F | 392.65 |
a | Hexadecimal floating point, lowercase | -0xc.90fep-2 |
A | Hexadecimal floating point, uppercase | -0XC.90FEP-2 |
c | Character | a |
s | String of characters | sample |
p | Pointer address | b8000000 |
n | Nothing printed. The corresponding argument must be a pointer to a signed int. The number of characters written so far is stored in the pointed location. |
|
% | A % followed by another % character will write a single % to the stream. | % |
flags | description |
---|---|
- | Left-justify within the given field width; Right justification is the default (see width sub-specifier). 左对齐 |
+ | Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign. 右对齐 |
(space) | If no sign is going to be written, a blank space is inserted before the value. 输出值为正时冠以空格,为负时冠以负号 |
# | Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written. 对o类,在输出时加前缀o;对x/X类,在输出时加前缀0x/0X;对e、g、f 类当结果有小数时才给出小数点。 |
0 | Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier). |
width | description |
---|---|
(number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. 若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0。 |
* | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
.precision | description |
---|---|
.number |
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. 如果输出数字,则表示小数的位数;如果输出的是字符,则表示输出字符的个数;若实际位数大于所定义的精度数,则截去超过的部分。 |
.* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x X | f F e E g G a A | c | s | p | n |
(none) | int | unsigned int | double | int | char* | void* | int* |
hh | signed char | unsigned char | signed char* | ||||
h | short int | unsigned short int | short int* | ||||
l | long int | unsigned long int | wint_t | wchar_t* | long int* | ||
ll | long long int | unsigned long long int | long long int* | ||||
j | intmax_t | uintmax_t | intmax_t* | ||||
z | size_t | size_t | size_t* | ||||
t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
L | long double |
c
specifier: it takes an int (or wint_t) as argument, but performs the proper conversion to a char value (or a wchar_t) before formatting it for output.
On success, the total number of characters written is returned.
If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.
If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.
1.如果想显示数值的前缀,可以使用说明符 ’%#n‘, ‘%#x‘, ‘%#X‘分别生成 ‘0’, ‘0x‘, ‘0X‘ 前缀。
2.对于scanf函数,需求%s类型时,“\n”是不会影响scanf内容的,对于需求%c类型时,\n也是字符,自然会有影响. 参考博文scanf函数和回车、空格 及其返回值
scanf函数的结束通常有3种,所谓的whitespace:遇到空格、回车或者tab键;或者按照格式控制符的指定来控制结束,如%5d类的格式;遇到非法输入也会自动结束。
对常用的三种格式,结束符号分别如下:
%d格式,默认分隔符是所有的 white-spaces(空格、回车、制表);
%c格式,则按ASCII字符考虑,无分隔符。可能会受到之前输入的影响,必要时用fflush(stdin);清除缓冲区,就是说whitespace也会被scanf读进;
%s格式,默认分隔符是所有的 white-spaces,输入后自动加入结束符"\0"。
标签:www. bsp stdin tput mat count floating 说明 arch
原文地址:http://www.cnblogs.com/Atanisi/p/7525951.html