C library function -printf()

C Standard Library - <stdio.h>

Description

C Library Functionsint printf(const char *format, ...)Send formatted output to the standard output stdout.

The calling format of the printf() function is:

printf("<格式化字符串>", <参量表>);

Declaration

Below is the declaration of the printf() function.

int printf(const char *format, ...)

Parameter

  • format-- This is the string containing the text to be written to the standard output stdout. It can contain embedded format tags, which can be replaced by the values specified in the following additional arguments and formatted as required. The format tag attributes are: %[flags][width][.precision][length]specifierThe detailed explanation is as follows:

Format character Meaning
a, A

Output floating-point numbers in hexadecimal form (new in C99).

Exampleprintf("pi=%a\n", 3.14);Outputpi=0x1.91eb86p+1。

d Output signed integers in decimal form (no sign for positive numbers).
o Output unsigned integers in octal form (without the prefix 0).
x,X Output unsigned integers in hexadecimal form (without the prefix 0x).
u Outputs an unsigned integer in decimal form
f Outputs single/double precision real numbers in fractional form
e,E Outputs single/double precision real numbers in exponential form
g,G Output single- or double-precision real numbers using the shorter output width of %f or %e.
c Output a single character
s Output string
p Output pointer address
lu 32-bit unsigned integer
llu 64-bit unsigned integer

flagsDescription
-Left-align within the given field width; the default is right-alignment (see the width sub-specifier).
+Forces a plus or minus sign (+ or -) to be displayed before the result, i.e., positive numbers will show a + sign before them. By default, only negative numbers have a - sign displayed before them.
spaceIf no sign is written, insert a space before the value.
#When used with the o, x, or X specifiers, non-zero values are preceded by 0, 0x, or 0X respectively.
When used with e, E, and f, it forces the output to contain a decimal point, even if there are no digits after it. By default, the decimal point is not displayed if there are no following digits.
When used with g or G, the result is the same as when using e or E, but trailing zeros will not be removed.
0Place zeros (0) to the left of the number to fill the specified padding, instead of spaces (see the width sub-specifier).

widthDescription
(number)The minimum number of characters to be output. If the output value is shorter than this number, the result will be padded with spaces. If the output value is longer than this number, the result will not be truncated.
*The width is not specified in the format string, but is supplied as an additional integer value argument placed before the argument to be formatted.

.precisionDescription
.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the written value is shorter than this number, the result will be padded with leading zeros. If the written value is longer than this number, the result will not be truncated. A precision of 0 means no characters are written.
For the e, E, and f specifiers: the number of digits to be output after the decimal point.
For the g and G specifiers: the maximum number of significant digits to be output.
For s: the maximum number of characters to be output. By default, all characters are output until the terminating null character is encountered.
For the c type: has no effect.
When no precision is specified, the default is 1. If it is specified without an explicit value, it is assumed to be 0.
.*The precision is not specified in the format string, but is supplied as an additional integer value argument placed before the argument to be formatted.

lengthDescription
hThe argument is interpreted as a short integer or unsigned short integer (applicable only to the integer specifiers: i, d, o, u, x, and X).
lThe argument is interpreted as a long integer or unsigned long integer, applicable to the integer specifiers (i, d, o, u, x, and X) and to the specifiers c (meaning a wide character) and s (meaning a wide-character string).
LThe argument is interpreted as a long double (applicable only to the 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, returns the total number of characters written; otherwise, returns a negative number.

Example

The following example demonstrates the usage of the printf() function.

Example

#include <stdio.h> int main () { int ch; for( ch = 75 ; ch <= 100; ch++ ) { printf("ASCII value = %d, character = %c\n", ch , ch ); } return(0); }

Let us 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.example.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 output result of execution is:

字符为 A 
字符串为 www.example.com 
浮点数为 10.234000 
整数为 150
双精度值为 20.123456 
八进制值为 226 
十六进制值为 96 

Specifier

  • %dSigned decimal integer
  • %uUnsigned decimal integer
  • %fFloating-point number
  • %sString
  • %cSingle character
  • %pPointer value
  • %eFloating-point number in exponential form
  • %x, %XUnsigned integer represented in hexadecimal
  • %oUnsigned integer represented in octal
  • %gOutput the value using the format (%e or %f) that gives the shorter output length.
  • %pOutput address specifier
  • %lu32-bit unsigned integer
  • %llu64-bit unsigned integer
  • %%Outputs the percent sign character itself.

In addition to the format specifiers, the printf() function also supports flags and options for controlling output precision, width, padding characters, alignment, etc. For example:

  • %-10s: a left-aligned string occupying a width of 10;
  • %5.2f: a right-aligned floating-point number occupying a width of 5, with two decimal places;
  • %#x: output a hexadecimal number with the 0x prefix.

The printf() function also supports variable-length argument lists, which can be denoted by an ellipsis...to specify multiple values to be output. Inside the function, the va_start() and va_end() macros can be used to access the values in the variable-length argument list. For example:

Example

#include <stdio.h>
#include <stdarg.h>

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 output result of execution is:

1 2 3

C Standard Library - <stdio.h>

other extensions