C library function - printf()
Description
C library function *int printf(const char format, ...) Sends formatted output to the standard output stdout.
The call format of the printf() function is:
printf("<格式化字符串>", <参量表>);
Declaration
The following is the declaration of the printf() function.
int printf(const char *format, ...)
Parameters
-
format -- This is a string containing the text to be written to the standard output stdout. It can contain embedded format tags, which are replaced by values specified in subsequent additional arguments and formatted as required. The format tag attributes are %[flags][width][.precision][length]specifier, explained in detail below:
| Format character | Significance |
|---|---|
| a, A |
Outputs a floating-point number in hexadecimal form (added in C99). Example printf("pi=%a\n", 3.14); Output pi=0x1.91eb86p+1. |
| d | Outputs a signed integer in decimal form (no sign output for positive numbers) |
| o | Outputs an unsigned integer in octal form (without the leading 0 prefix) |
| x,X | Outputs an unsigned integer in hexadecimal form (without the 0x prefix) |
| u | Outputs an unsigned integer in decimal form |
| f | Outputs single- and double-precision real numbers in decimal form |
| e,E | Outputs single- and double-precision real numbers in exponential form |
| g,G | Outputs single- and double-precision real numbers using the shorter of %f or %e |
| c | Outputs a single character |
| s | Output the string |
| p | Outputs a pointer address |
| lu | 32-bit unsigned integer |
| llu | 64-bit unsigned integer |
| flags | Description |
|---|---|
| - | Left-justify within the given field width; right alignment is the default (see the width sub-specifier). |
| + | Force a plus or minus sign (+ or -) to appear before the result; that is, a + sign is shown before positive numbers. By default, only negative numbers are preceded by a - sign. |
| Space | If no sign is to be written, a space is inserted before the value. |
| # | When used with o, x, or X specifiers, a nonzero value is prefixed with 0, 0x, or 0X, respectively. |
与 e、E 和 f 一起使用时,会强制输出包含一个小数点,即使后边没有数字时也会显示小数点。默认情况下,如果后边没有数字时候,不会显示显示小数点。
与 g 或 G 一起使用时,结果与使用 e 或 E 时相同,但是尾部的零不会被移除。 |
| 0 | Place zeros (0) to the left of the number for padding instead of spaces (see the width sub-specifier). |
| width (width) | Description |
|---|---|
| (number) | The minimum number of characters to be output. If the value to be output is shorter than this number, the result is padded with spaces. If the value is longer than this number, the result is not truncated. |
| * | The width is not specified in the format string but is placed as an additional integer argument before the argument 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. If the value is longer than this number, the result is not truncated. A precision of 0 means no characters are written. |
对于 e、E 和 f 说明符:要在小数点后输出的小数位数。
对于 g 和 G 说明符:要输出的最大有效位数。
对于 s: 要输出的最大字符数。默认情况下,所有字符都会被输出,直到遇到末尾的空字符。
对于 c 类型:没有任何影响。
当未指定任何精度时,默认为 1。如果指定时不带有一个显式值,则假定为 0。 |
| .* | The precision is not specified in the format string but is placed as an additional integer argument before the argument to be formatted. |
| length | Description |
|---|---|
| h | The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x, and X). |
| l | The argument is interpreted as a long int or unsigned long int, applying to integer specifiers (i, d, o, u, x, and X) and to the specifiers c (a wide character) and s (a wide-character string). |
| L | The argument is interpreted as a long double (only applies to floating-point specifiers: e, E, f, g, and G). |
-
Additional arguments -- Depending on the format string, the function may require a series of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format argument. The number of arguments should be the same as the number of % tags.
Return Value
If successful, it returns the total number of characters written; otherwise, it returns a negative number.
Example
The following example demonstrates the use of the printf() function.
Example
#include
<
stdio.h
int
main
(
)
{
int
ch;
for
(
ch
=
75
;
ch
<=
100
;
ch
++
)
{
printf
(
"
ASCII value = %d, char = %c
\
n
"
,
ch
,
ch
)
;
}
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
ASCII 值 = 75, 字符 = K
ASCII 值 = 76, 字符 = L
ASCII 值 = 77, 字符 = M
ASCII 值 = 78, 字符 = N
ASCII 值 = 79, 字符 = O
ASCII 值 = 80, 字符 = P
ASCII 值 = 81, 字符 = Q
ASCII 值 = 82, 字符 = R
ASCII 值 = 83, 字符 = S
ASCII 值 = 84, 字符 = T
ASCII 值 = 85, 字符 = U
ASCII 值 = 86, 字符 = V
ASCII 值 = 87, 字符 = W
ASCII 值 = 88, 字符 = X
ASCII 值 = 89, 字符 = Y
ASCII 值 = 90, 字符 = Z
ASCII 值 = 91, 字符 = [
ASCII 值 = 92, 字符 = \
ASCII 值 = 93, 字符 = ]
ASCII 值 = 94, 字符 = ^
ASCII 值 = 95, 字符 = _
ASCII 值 = 96, 字符 = `
ASCII 值 = 97, 字符 = a
ASCII 值 = 98, 字符 = b
ASCII 值 = 99, 字符 = c
ASCII 值 = 100, 字符 = d
The following example outputs various formatted data:
Example
#include
<
stdio.h
int
main
(
)
{
char
ch= '
A
';
char
str
[
20
]
=
"
www.runoob.com
"
;
float
flt
=
10
.234
;
int
no
=
150
;
double
dbl
=
20
.123456
;
printf
(
"
Character is %c
\
n
"
,
ch
)
;
printf
(
"
String is %s
\
n
"
,
str
)
;
printf
(
"
Floating-point number is %f
\
n
"
,
flt
)
;
printf
(
"
Integer is %d
\
n
"
,
no
)
;
printf
(
"
Double-precision value is %lf
\
n
"
,
dbl
)
;
printf
(
"
Octal value is %o
\
n
"
,
no
)
;
printf
(
"
Hexadecimal value is %x
\
n
"
,
no
)
;
return
0
;
}
The execution output is:
字符为 A
字符串为 www.runoob.com
浮点数为 10.234000
整数为 150
双精度值为 20.123456
八进制值为 226
十六进制值为 96
Specifier
- %d Decimal signed integer
- %u Decimal unsigned integer
- %f Float (floating point number)
- %s String
- %c Single character
- %p Pointer value
- %e Floating-point number in exponential form
- %x, %X Unsigned integer in hexadecimal
- %o Unsigned integer in octal
- %g Outputs the value using whichever of %e or %f produces the shorter output
- %p Outputs the address symbol
- %lu 32-bit unsigned integer
- %llu 64-bit unsigned integer
- %% Outputs the percent sign character itself.
In addition to format specifiers, the printf() function also supports flags and options for controlling output precision, width, padding characters, and alignment. For example:
- %-10s: left-aligned string occupying a width of 10;
- %5.2f: right-aligned floating-point number occupying a width of 5, with two decimal places;
- %#x: outputs a hexadecimal number with a 0x prefix.
The printf() function also supports variable-length argument lists, which can be denoted by an ellipsis ... to specify multiple values to output. Inside the function, the va_start() and va_end() macros can be used to access the values in the variable argument list. For example:
Example
#include
#include
void
print_values
(
int
count
,
...
)
{
va_list args
;
va_start
(
args
,
count
)
;
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
printf
(
"%d "
,
va_arg
(
args
,
int
)
)
;
}
va_end
(
args
)
;
}
int
main
(
)
{
print_values
(
3
,
1
,
2
,
3
)
;
// Output "1 2 3"
return
0
;
}
The execution output is:
1 2 3
C Standard Library -