Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
fprintf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
fprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
fwprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
fwprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
printf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
printf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
snprintf() |
Print formatted text | (·) | <stdio.h> |
C99 | C11 | ||||||
snprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
snwprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
sprintf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
sprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
swprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
swprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vfprintf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
vfprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vfwprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
vfwprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vprintf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
vprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vsnprintf() |
Print formatted text | (·) | <stdio.h> |
C99 | C11 | ||||||
vsnprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vsnwprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vsprintf() |
Print formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
vsprintf_s() |
Print formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vswprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
vswprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vwprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
vwprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
wprintf() |
Print formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
wprintf_s() |
Print formatted text | ? | (·) | <wchar.h> |
C11 |
C provides a family of functions for converting characters, strings and numbers into character sequences:
#include<stdio.h>
int printf(const char *fmt, ...); int fprintf(FILE *fp, const char *fmt, ...); int sprintf(char *buf, const char *fmt, ...); int snprintf(char *buf, size_t len, const char *fmt, ...); #include<stdarg.h>
int vprintf(const char *fmt, va_list va); int vfprintf(FILE *fp, const char *fmt, va_list va); int vsprintf(char *buf, const char *fmt, va_list va); int vsnprintf(char *buf, size_t len, const char *fmt, va_list va);
#include<wchar.h>
int wprintf(const wchar_t *fmt, ...); int swprintf(wchar_t *buf, size_t len, const wchar_t *fmt, ...); #include<stdio.h>
int fwprintf(FILE *fp, const wchar_t *fmt, ...);
#include<wchar.h>
#include<stdarg.h>
int vwprintf(const wchar_t *fmt, va_list va); int vswprintf(wchar_t *buf, size_t len, const wchar_t *fmt, va_list va); #include<stdio.h>
int vfwprintf(FILE *fp, const wchar_t *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
int printf_s(const char *fmt, ...); int fprintf_s(FILE *fp, const char *fmt, ...); int sprintf_s(char *buf, rsize_t len, const char *fmt, ...); int snprintf_s(char *buf, rsize_t len, const char *fmt, ...); #include<stdarg.h>
int vprintf_s(const char *fmt, va_list va); int vfprintf_s(FILE *fp, const char *fmt, va_list va); int vsprintf_s(char *buf, rsize_t len, const char *fmt, va_list va); int vsnprintf_s(char *buf, rsize_t len, const char *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <wchar.h>
int wprintf_s(const wchar_t *fmt, ...); int swprintf_s(wchar_t *buf, rsize_t len, const wchar_t *fmt, ...); int snwprintf_s(wchar_t *buf, rsize_t len, const wchar_t *fmt, ...); #include<stdarg.h>
int vwprintf_s(const wchar_t *fmt, va_list va); int vswprintf_s(wchar_t *buf, rsize_t len, const wchar_t *fmt, va_list va); int vsnwprintf_s(wchar_t *buf, rsize_t len, const wchar_t *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
#include<wchar.h>
int fwprintf_s(FILE *fp, const wchar_t *fmt, ...); #include<stdarg.h>
int vfwprintf_s(FILE *fp, const wchar_t *fmt, va_list va);
Each function takes a format string fmt
and a variable number of arguments in one form
or another, which are consumed in sequence according to the
format string. It also has a defined output destination,
which may be an open stream or a character array. (In the
latter case, a terminating null character is also written to
the array.) Under normal circumstances, each function returns
the number of bytes or characters written, excluding any
terminating null character.
Each function prints characters from the format string to the destination, unless it encounters a % (U+0025), indicating the start of a conversion specifier. For each conversion specifier, the function consumes an argument, and converts it into a sequence of characters sent instead of the conversion specifier to the destination. The conversion specifier specifies the expected type of the argument, and the format of its representation as a sequence of characters.
Each conversion specifier begins with %, and
continues with optional flags #, 0,
- and +, then an optional integer
field width, then an optional precision (a dot .
(U+002E)
followed by an integer), then an optional type modifier (one
of "hh"
, "h"
,
"l"
, "ll"
,
"j"
, "z"
,
"t"
and "L"
),
and finally a format code (one of "d"
,
"i"
, "u"
,
"o"
, "x"
,
"X"
, "a"
,
"A"
, "e"
,
"E"
, "f"
,
"F"
, "g"
,
"G"
, "c"
,
"s"
, "p"
and
"n"
).
Format code | Format | Argument type for each type modifier | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
hh | h | None | l | ll | j | z | t | L | |||
From C99 | From C99 | ||||||||||
d | Signed base-10 integer | int |
long |
long long |
intmax_t |
ptrdiff_t |
N/A | ||||
i | |||||||||||
u | Unsigned base-10 integer | unsigned |
unsigned long |
unsigned long long |
uintmax_t |
size_t |
|||||
o | Unsigned base-8 integer | ||||||||||
x | Unsigned base-16 integer, lower-case | ||||||||||
X | Unsigned base-16 integer, upper-case | ||||||||||
a | From C99 | Base-16 real, lower-case | N/A | double |
N/A | N/A | long double |
||||
A | Base-16 real, upper-case | ||||||||||
e | Exponential decimal, lower-case | ||||||||||
E | Exponential decimal, upper-case | ||||||||||
f | Fixed decimal, lower-case | ||||||||||
F | Fixed decimal, upper-case | ||||||||||
g | General decimal, lower-case | ||||||||||
G | General decimal, upper-case | ||||||||||
c | Character | int |
wint_t From C95 |
N/A | |||||||
s | String | char
* |
wchar_t * From C95 |
||||||||
p | Pointer | void
* |
N/A | ||||||||
n | None; store count of characters written so far | signed
char
* |
short
* |
int * |
long * |
long long * |
|||||
% | Literal % |
N/A |
Only lower-case letters might acquire new meanings in conversion specifiers.
Note that arguments of type char
/signed char
/unsigned char
and
short
/unsigned short
are
promoted to int
/unsigned
when
passed through a variable-length argument list, and
float
is
promoted to double
, so
these types do not need type modifiers. Nevertheless, the
modifiers used for scanf
are also provided for printf
,
for the sake of duality.
The field width controls padding. If a conversion specifier expands to less than the width, the output sequence is padded, with spaces by default, to fill the field width. The 0 flag causes padding with zeroes on numerical fields. The - flag inserts padding on the right, rather than on the left by default.
printf("x%10gy\n", 1.0); printf("x%-10gy\n", 1.0); printf("x%010gy\n", 1.0);
x 1y x1 y x0000000001y
The precision indicates the number of decimal places to use for numerical fields. For string fields, it indicates the length of a prefix of the string to be used, discarding the rest.
printf("%.4f\n", 1.0);
1.0000
The + flag with numerical fields causes a leading sign character to be printed. The # flag causes hexadecimal fields to be prefixed with 0x, and octal fields with 0.
These functions return a negative value on error.
Thirty-or-so functions exist because C does not support any name overloading, yet there are many independent options that must be selected. The form of the name is {v}{f,s,sn}{w}printf{_s}.
-
The first part v indicates that the argument list is supplied as a
va_
given as the last argument. Without v, the function is declared withlist ...
at the end of its parameter list, so arguments are given directly. -
The second part f,s,sn selects the output.
- s or sn indicates that
characters are to be written to an array, followed by a
null terminator. The first argument
buf
is a pointer to the first element to be written to. -
f indicates that characters are to be
written to a stream,
FILE *fp
, which is given as the first argument. The number of characters written is returned. - Functions with none of these prefixes write to the
standard output stream,
stdout
.
- s or sn indicates that
characters are to be written to an array, followed by a
null terminator. The first argument
-
The third part w indicates that the function operates on wide characters:
- The format string is a null-terminated array of
wchar_t
. - s and sn functions write
to arrays of
wchar_t
. -
f and
stdout
functions are wide-oriented.
Without w, the function operates on multibyte characters:
- The format string is a null-terminated array of
char
. - s and sn functions write
to arrays of
char
. -
f and
stdout
functions are byte-oriented.
- The format string is a null-terminated array of
-
sn, sw and s..._s functions take an additional integer argument
len
, indicating the size of the output array. Characters are only written to elementsbuf[0]
tobuf[len-1]
:- If the number of characters (including the null
terminator) to be written does not exceed
len
, the number of characters (excluding the null terminator) written is returned. - Otherwise:
- sw and s..._s
functions return negative (or specifically
-1
). - sn functions return the required
array size excluding the null terminator. The array
buf[0]
tobuf[len-1]
, iflen > 0
, will contain truncated output, including a null terminator.
- sw and s..._s
functions return negative (or specifically
sprintf
andvsprintf
take no length argument. They must only be used when the output is guaranteed to fit in the array. - If the number of characters (including the null
terminator) to be written does not exceed
-
_s functions apply additional bounds checking, and fail on other constraint violations:
- %n as a conversion specifier;
NULL
as an argument corresponding to %s.
If the output is too long for the array
buf[0]
tobuf[len-1]
, the s..._s functions setbuf[0] = 0
, providedbuf != NULL
andlen > 0
.