The usage of input output conversion specifier in C language

Time:2021-6-9

The usage of input output conversion specifier in C language

This paper mainly introduces the usage of conversion specification in formatted input and output statements in C language

printf sprintf fprintfConversion specifier for

% [flags][width][.precision][length]specifier

flags meaning
Align left
+ When the signed value is positive, a positive sign is added before the value; If negative, precede the value with a negative sign
space When the signed value is positive, a space is displayed in front of the value; If negative, precede the value with a negative sign
0 For numerical format, 0 is used to replace the characters with insufficient space; For integer format, if you use-Flag or specified precision, the flag does not work
# Convert the result to another format.
If it is% O, start with 0;
If it is% X or% x, it will start with 0 X or 0 X;
For all floating-point types, the decimal point is mandatory;
For% g or% G format, make it contain trailing 0
width meaning .precision meaning
number OutputminimumLength (number of characters) .number The accuracy of the output.
For% e, e and% F, the number of digits to the right of the decimal point
For% G and% G, the maximum number of significant digits
For% s, the maximum number of characters to be printed
For integer, the minimum number of digits to be printed
Used alone.And.0It has the same effect
* _ number_ The value of is determined by the corresponding variable in the list of items to be printed .* _ number_ The value of is determined by the corresponding variable in the list of items to be printed

The combination of length and specifier determines how to interpret the data type of the corresponding variable in the list to be printed. The combination relationship is shown in the following table

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

Note:

(1) intmax_tanduintmax_tDefined instdint.hIs a 128 bit integer type

(2) size_tDefined instddef.hMedium(stdio.hThe header file is already included)sizeof()The type of the value of

(3) ptrdiff_tIs a type that represents the difference between two pointers

specifier be used for specifier be used for
I or D int A or a Hexadecimal floating point number
u unsigned int c char
o Unsigned octal integer C ISO wide character type (wchar)_ t)
X or X Unsigned hexadecimal integer (x in uppercase) s Char * string
F or F Floating point type (six decimal places by default) p Pointer (output hexadecimal address)
E or e Display as scientific counting method (keep six decimal places) n No characters will be output, but the number of characters output before the position will be stored in the variable indicated by the corresponding pointer (default is int *)
G or G According to different values, it can automatically choose whether to use scientific counting method (excluding trailing 0). Shorter length is preferred % Output character ‘%’

scanf sscanf fscanfConversion specifier for

%[*][width][length]specifier

*And width meaning
* Skip the corresponding input
(for example, in the statementscanf("%*d %*d %d",&n);Only the third integer read is stored in the variablenIn, the first two values are ignored)
number Maximum number of characters. The input stops when the maximum number of characters is reached or when a blank character is encountered for the first time
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*
specifier be used for
i Interpret the input as an integer, and automatically identify the hexadecimal system (if the input starts with X, it will be hexadecimal, if the input starts with 0, it will be octal, if the input starts with 1 to 9, it will be decimal)
D or u Interpret input as a decimal integer (D for signed, u for unsigned)
o Interpret the input as a signed octal integer
x Interpret the input as a signed hexadecimal integer
s String (all characters from the first non whitespace character to the next whitespace character are input)
p Interpret input as a pointer (address)
a,e,f,g Interpret input as a floating point number (decimal or hexadecimal)
c Interpret input as char (don’t ignore white space)
[chatacters]And[^characters] Scan set, read only the characters contained in [], exclude the characters contained in [^], stop at the first position that does not match the characters in [] (or matches the characters in [^]), and finally read the characters to form a string, which is stored in the continuous memory space indicated by the corresponding pointer in the list.
Note: only for character type variables