Names specified here
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_LIB_EXT1__ 1
#include <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_LIB_EXT1__ 1
#include <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_LIB_EXT1__ 1
#include <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").

Conversion specifiers for printf
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}.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2024-06-09T11:09:27.427+0000