C - printf() function

Introduction

The primary function for formatted output to the stdout stream is printf().

The stdio.h header may also declare the optional printf_s() function, which is a safe version of printf().

printf_s() does not permit the %n output specification to be included in the format string.

%n output writes data to memory, and this makes it unsafe.

You must define the symbol __STDC_WANT_LIB_EXT1__ as 1 prior to the #include directives for the library headers to make the optional safe library functions available.

The prototype of the printf_s() function is:

int printf_s(char *format, . . .);

The first parameter is the format control string.

The optional arguments to the function are the values to be output in sequence.

They must correspond in number and type with the format conversion specifiers that appear in the format string.

The following table shows how the optional output flag characters affect the output.

Character
Use
+

Ensures there's always a plus or minus sign preceding a signed output value.
Only negative values have a sign by default.
-

the output value is left justified and padded with blanks on the right.
The default positioning of the output is right justified.
0

Specifies that integer or floating point values should be padded with zeros instead of spaces to fill
out the field width to the left.
#




Ensures that:
0 is to precede an octal output value,
0x or 0X is to precede a hexadecimal output value,
a floating-point output value will contain a decimal point,
for g or G floating-point conversion characters, trailing zeros will not be omitted.
Space
Specifies that positive or zero values are preceded by a space rather than a plus sign.

The optional field_width specifies the minimum field width for the output value.

If the value requires more characters, the field is simply expanded.

If the field width is preceded by the 0 flag, as in 09, for example, the output would be filled on the left with zeros.

The optional precision specifier is used with floating-point output values and consists of a period followed by an integer.

A specifier of .n indicates that n decimal places are to be output for a floating-point value.

If the value to be output has more than n significant digits, it's rounded.

If you use it for integer conversions, it specifies the minimum number of digits to appear in the output.

The optional size flags are shown in the following table.

Flag
Effect
h
A following integer conversion specifier applies to a short or unsigned short argument.
hh

A following integer conversion specifier applies to a signed char or
unsigned char argument.
l (lowercase L)
A following integer conversion specifier applies to a long or unsigned long argument.
ll (lowercase LL)
A following integer conversion specifier applies to a long long or unsigned long long argument.
j
A following integer conversion specifier applies to an intmax_t or uintmax_t argument.
z

A following integer conversion specifier applies to a size_t argument. This flag avoids potential
compiler warnings that can arise because size_t is an implementation-defined integer type.
t
A following integer conversion specifier applies to a ptrdiff_t argument.
L
A following floating-point conversion specifier applies to a long double argument.

The conversion character defines how the output is to be converted for a particular type of value.

Integers Conversion characters are defined in the following table.

Conversion character
Output produced
d or i
Signed decimal integer
o
Unsigned octal integer
u
Unsigned decimal integer
x
Unsigned hexadecimal integer with lowercase hexadecimal digits a, b, c, d, e, f
X
As x but with uppercase hexadecimal digits A, B, C, D, E, F

Floating-point Conversion characters are defined in the following table.

Conversion character
Output produced
f or F
Signed decimal value
e
Signed decimal value with exponent
E
As e but with E for exponent instead of e
g
As e or f depending on size of value and precision
G
As g but with E for exponent values
a or A


Presents a double value in hexadecimal form with the hexadecimal
mantissa value preceded by 0x or 0X and any exponent prefixed with p or
P, thus: 0xh.hhhhp+/-d where h is a hexadecimal digit.

Pointers Conversion characters are defined in the following table.

Conversion character
Output produced
p

Output the value of the argument as a pointer and the value is presented in
an implementation-defined way. The argument should be of type void*.

Characters Conversion characters are defined in the following table.

Conversion character
Output produced
c
Single character or precision characters
s
All characters until '\0' is reached or precision characters have been output

The %n specifier only works with printf().

The corresponding argument must be of type int*, and the effect of %n is to store the number of characters written to stdout so far.