English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C format conversion specifier

Format specifiers are strings used in formatted input and output functions. The format string determines the format of input and output. The format string always starts with the '%%' character.

1. Conversion description and output as a result

Conversion description

Output

%a

Floating-point number, hexadecimal digits and p-Notation (C99)

%A

Floating-point number, hexadecimal digits and P-Notation (C99)

%c

One character

%d

Signed decimal integer

%e

Floating-point number, e-Notation

%E

Floating-point number, E-Notation

%f

Floating-point number, decimal notation

%g

Automatically select %f or %e based on the value. The %e format is used when the exponent is less than-4or use when greater than or equal to precision

%G

Automatically select %f or %E based on the value. The %E format is used when the exponent is less than-4or use when greater than or equal to precision

%i

Significant decimal integer (same as %d)

%o

unsigned octal integer

%p

pointer (which is the address)

%s

string

㩵n

unsigned decimal integer

%x

unsigned hexadecimal integer using hexadecimal digits 0f

%X

unsigned hexadecimal integer using hexadecimal digits 0F

%%

prints a percent sign

Second, printf() modifiers

Modifier

Meaning

flags

five flags (-+flags (such as space, #, and 0) will be described in Table 3, and zero or more flags

digit(s)

minimum field width. If the field cannot accommodate the number or string to be printed, the system will use a wider field. Example:"4d

.digit(s)

precision. For %e, %E, and %f conversions, it is the number of digits to be printed to the right of the decimal point. For %g and %G conversions, it is the maximum number of significant digits. For %s conversion, it is the maximum number of characters to be printed. For integer conversions, it is the minimum number of digits to be printed; if necessary, leading zeros are used to reach this number. Only the." indicates that a zero follows, so %.f is the same as %.0f. Example:"5.2prints a floating-point number, whose field width is5two digits after the decimal point.

h

used together with integer conversion specifiers to represent ashort int or unsigned short int type numeric value.

Example: "%hu", "%hx" and"6.4hd"

hh

used together with integer conversion specifiers to represent asigned char orunsigned chartype numeric value.

Example: "%hhu", "%hhx" and"6.4hhd"

j

used together with integer conversion specifiers to represent an intmax_t or uintmax_t value.

Example: "%jd" and"8jX"

l

used together with integer specifiers to represent along int orunsigned long int type value.

Example: "%ld" and"8lu"

ll

used together with integer specifiers to represent along long intor unsigned long long int type value (C99)

Example: "%lld" and"8llu"

L

used together with floating-point conversion specifiers to represent along doublevalue.

Example: "%Lf" and"10.4Le"

t

used together with integer conversion specifiers to represent a ptrdiff_t value (the type corresponding to the difference between two pointers) (C99)

Example:""%td" and"%%12ti"

z

used together with integer conversion specifiers to represent asize_tvalue(sizeof returns the type) (C99)

Example:""%zd" and"%%12zx"

Three The symbol of printf()

Modifier

Meaning

-

The project is aligned to the left, meaning that the project will be printed starting from the left side of the field. Example: "%%"-20s

+

If the signed value is positive, it will display a plus sign; if it is negative, it will display a minus sign. Example: "%+6.2f

(space)

If the signed value is positive, it will display a leading space (but not the sign); if it is negative, it will display a minus sign.+The flag will override the space flag. Example: "% 6.2f

#

The optional form of the conversion specifier. If it is %o format, it starts with 0; if it is %x and %X format, it starts with 0x or 0X, for all floating-point formats, # ensures that a decimal point character is printed even if no digits are limited. For %g and %G formats, it prevents trailing zeros from being deleted. Example: "%#o", "%#8.0f" and "%+#10.3E

0

For all numeric formats, fill the field width with leading zeros instead of spaces. If there is-Flag or specified precision (for integers) will ignore this flag. Example: "%010d" and "%08.3f

Four, scanf() Conversion Specifiers in ANSI C

Conversion specifier

Meaning

%c

Interpret the input as a character

%d

Interpret the input as a signed decimal integer

%e, %f, %g, %a

Interpret the input as a floating-point number (%a is C99Standard)

%E, %F, %G, %A

Interpret the input as a floating-point number (%A is C99Standard)

%i

Interpret the input as a signed decimal integer

%o

Interpret the input as aSignedOctal number

%p

Interpret the input as a pointer (address)

%s

㩵n

Interpret the input as an unsigned decimal integer

%x, %X

Interpret the input as aSignedHexadecimal integer

Five, scanf() Conversion Modifiers

Modifier

Meaning

*

Postfix assignment. Example: "%"*d

digit(s)

Maximum field width; stop reading the input item when the maximum field width is reached or the first whitespace character is encountered (whichever comes first). Example: "%"10s

hh

Read the integer as signed char or unsigned char. Example: "%hhd", "%hhu"

ll

Read the integer as long long or unsigned long long (C99). Example: "%lld", "%llu"

h, l, or L

"%hd" and "hi" indicate that the value will be stored in a short int.

"%ho", "%hx", and "%hu" indicate that the value will be stored in an unsigned short int.

"%ld" and "%li" indicate that the value will be stored in a long.

"%lo", "%lx", and "%lu" indicate that the value will be stored in an unsigned long.

"%le", "%lf", and "%lg" indicate that the value is stored as double type. Use L (not l) with e, f, and g to indicate that the value is stored as long double type.

If these modifiers are not present, d, i, o, and x indicate int type, while e, f, and g indicate float type.