C
Language | ||||
headers | ||||
Type support | ||||
Dynamic memory management | ||||
Error handling | ||||
Program utilities | ||||
Variadic function support | ||||
Date and time utilities | ||||
Strings library | ||||
Algorithms | ||||
Numerics | ||||
Input/output support | ||||
Localization support | ||||
Thread support (C11) | ||||
Atomic operations (C11) | ||||
Technical Specifications |
File input/output
Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
File access | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Direct input/output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fread | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fwrite | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Unformatted input/output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Formatted input | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Formatted output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
File positioning | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ftell | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fgetpos | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fseek | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fsetpos | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewind | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Error handling | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clearerr | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
feof | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ferror | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
perror | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on files | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
remove | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rename | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tmpfiletmpfile_s (C11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tmpnamtmpnam_s (C11) |
Defined in header | ||
(1) | ||
int printf( const char *format, ... ); | (until C99) | |
int printf( const char *restrict format, ... ); | (since C99) | |
(2) | ||
int fprintf( FILE *stream, const char *format, ... ); | (until C99) | |
int fprintf( FILE *restrict stream, const char *restrict format, ... ); | (since C99) | |
(3) | ||
int sprintf( char *buffer, const char *format, ... ); | (until C99) | |
int sprintf( char *restrict buffer, const char *restrict format, ... ); | (since C99) | |
int snprintf( char *restrict buffer, int bufsz, | (4) | (since C99) |
int printf_s(const char *restrict format, ...); | (5) | (since C11) |
int fprintf_s(FILE *restrict stream, const char *restrict format, ...); | (6) | (since C11) |
int sprintf_s(char *restrict buffer, rsize_t bufsz, | (7) | (since C11) |
int snprintf_s(char *restrict buffer, rsize_t bufsz, | (8) | (since C11) |
Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks.
1) Writes the results to the output stream stdout.
2) Writes the results to the output stream stream
.
3) Writes the results to a character string buffer
. The behavior is undefined if the string to be written (plus the terminating null character) exceeds the size of the array pointed to by buffer
.
4) Writes the results to a character string buffer
. At most bufsz
- 1 characters are written. The resulting character string will be terminated with a null character, unless bufsz
is zero. If bufsz
is zero, nothing is written and buffer
may be a null pointer, however the return value (number of bytes that would be written) is still calculated and returned.
5-8) Same as (1-4), except that the following errors are detected at runtime and call the currently installed constraint handler function:
- the conversion specifier
%n
is present informat
- any of the arguments corresponding to
%s
is a null pointer -
format
orbuffer
is a null pointer -
bufsz
is zero or greater than RSIZE_MAX - encoding errors occur in any of string and character conversion specifiers
- (for
sprintf_s
only), the string to be stored inbuffer
(including the trailing null) would be exceedbufsz
- the conversion specifier
- As all bounds-checked functions,
printf_s
,fprintf_s
,sprintf_s
, andsnrintf_s
are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including<stdio.h>
.
Parameters
stream | - | output file stream to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buffer | - | pointer to a character string to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bufsz | - | up to bufsz - 1 characters may be written, plus the null terminator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format | - | pointer to a null-terminated multibyte string specifying how to interpret the data. The format string consists of ordinary multibyte characters (except
The following format specifiers are available:
The floating point conversion functions convert infinity to Not-a-number is converted to The conversions Even though The correct conversion specifications for the fixed-width character types (int8_t, etc) are defined in the header <inttypes.h> (although PRIdMAX, PRIuMAX, etc is synonymous with The memory-writing conversion specifier %n is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked There is a sequence point after the action of each conversion specifier; this permits storing multiple %n results in the same variable or, as an edge case, printing a string modified by an earlier %n within the same call. If a conversion specification is invalid, the behavior is undefined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... | - | arguments specifying data to print |
Return value
1,2) number of characters transmitted to the output stream or negative value if an output error or an encoding error (for string and character conversion specifiers) occurred
3) number of characters written to buffer
(not counting the terminating null character), or a negative value if an encoding error (for string and character conversion specifiers) occurred
4) number of characters (not including the terminating null character) which would have been written to buffer
if bufsz
was ignored, or a negative value if an encoding error (for string and character conversion specifiers) occurred
5,6) number of characters transmitted to the output stream or negative value if an output error, a runtime constrants violation error, or an encoding error occurred.
7) number of characters written to buffer
, not counting the null character (which is always written as long as buffer
is not a null pointer and bufsz
is not zero and not greater than RSIZE_MAX
), or zero on runtime constraint violations, and negative value on encoding errors
8) number of characters not including the terminating null character (which is always written as long as buffer
is not a null pointer and bufsz
is not zero and not greater than RSIZE_MAX
), which would have been written to buffer
if bufsz
was ignored, or a negative value if a runtime constraints violation or an encoding error occurred
Notes
The C standard and POSIX specify that the behavior of sprintf
and its variants is undefined when an argument overlaps with the destination buffer. Example:
sprintf(dst, "%s and%s", dst, t); // <- broken: undefined behavior
POSIX specifies that errno is set on error. It also specifies additional conversion specifications, most notably support for argument reordering (n$ immediately after % indicates n
'th argument)
Calling snprintf
with zero bufsz
and null pointer for buffer
is useful to determine the necessary buffer size to contain the output:
const char *fmt = "sqrt(2) =%f";int sz = snprintf(NULL, 0, fmt, sqrt(2));char buf[sz + 1]; // note +1 for terminating null bytesnprintf(buf, sizeof buf, fmt, sqrt(2));
snprintf_s
, just like snprintf
, but unlike sprintf_s
, will truncate the output to fit in bufsz-1
.
Example
Run this code
#include <stdio.h>int main(void){ printf("Strings:\n"); const char* s = "Hello"; printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c%%\n", 65); printf("Integers\n"); printf("Decimal:\t%i%d%.6i%i%.0i%+i%u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x%x%X%#x\n", 5, 10, 10, 6); printf("Octal:\t%o%#o%#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f%.0f%.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t%05.2f%.2f%5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E%e\n", 1.5, 1.5); printf("Hexadecimal:\t%a%A\n", 1.5, 1.5);}
Output:
Strings: . Hello. .Hello . . Hello.Characters: A%IntegersDecimal: 1 2 000003 0 +4 4294967295Hexadecimal: 5 a A 0x6Octal: 12 012 04Floating pointRounding: 1.500000 2 1.30000000000000004440892098500626Padding: 01.50 1.50 1.50Scientific: 1.500000E+00 1.500000e+00Hexadecimal: 0x1.8p+0 0X1.8P+0
References
- C11 standard (ISO/IEC 9899:2011):
- 7.21.6.1 The fprintf function (p: 309-316)
- 7.21.6.3 The printf function (p: 324)
- 7.21.6.5 The snprintf function (p: 325)
- 7.21.6.6 The sprintf function (p: 325-326)
- K.3.5.3.1 The fprintf_s function (p: 591)
- K.3.5.3.3 The printf_s function (p: 593-594)
- K.3.5.3.5 The snprintf_s function (p: 594-595)
- K.3.5.3.6 The sprintf_s function (p: 595-596)
- C99 standard (ISO/IEC 9899:1999):
- 7.19.6.1 The fprintf function (p: 274-282)
- 7.19.6.3 The printf function (p: 290)
- 7.19.6.5 The snprintf function (p: 290-291)
- 7.19.6.6 The sprintf function (p: 291)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.9.6.1 The fprintf function
- 4.9.6.3 The printf function
- 4.9.6.5 The sprintf function
See also
wprintffwprintfswprintfwprintf_sfwprintf_sswprintf_ssnwprintf_s (C95)(C95)(C95)(C11)(C11)(C11)(C11) | prints formatted wide character output to stdout, a file stream or a buffer (function) |
vprintfvfprintfvsprintfvsnprintfvprintf_svfprintf_svsprintf_svsnprintf_s (C99)(C11)(C11)(C11)(C11) | prints formatted output to stdout, a file stream or a buffer using variable argument list (function) |
fputs | writes a character string to a file stream (function) |
scanffscanfsscanfscanf_sfscanf_ssscanf_s (C11)(C11)(C11) | reads formatted input from stdin, a file stream or a buffer (function) |
C++ documentation for printf, fprintf, sprintf, snprintf |