std::printf, std::fprintf, std::sprintf, std::snprintf
Defined in header <cstdio> | ||
|---|---|---|
int printf( const char* format, ... ); | (1) | |
int fprintf( std::FILE* stream, const char* format, ... ); | (2) | |
int sprintf( char* buffer, const char* format, ... ); | (3) | |
int snprintf( char* buffer, std::size_t buf_size, const char* format, ... ); | (4) | (since C++11) |
Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks.
stdout.stream.buffer.buffer. At most buf_size - 1 characters are written. The resulting character string will be terminated with a null character, unless buf_size is zero. If buf_size is zero, nothing is written and buffer may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.If a call to sprintf or snprintf causes copying to take place between objects that overlap, the behavior is undefined (e.g. sprintf(buf, "%s text", buf);).
Parameters
| stream | - | output file stream to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buffer | - | pointer to a character string to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buf_size | - | up to buf_size - 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 ( The memory-writing conversion specifier There is a sequence point after the action of each conversion specifier; this permits storing multiple If a conversion specification is invalid, the behavior is undefined. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ... | - | arguments specifying data to print. If any argument after default conversions is not the type expected by the corresponding conversion specifier, or if there are fewer arguments than required by format, the behavior is undefined. If there are more arguments than required by format, the extraneous arguments are evaluated and ignored |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return value
buf_size.Notes
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 std::snprintf with zero buf_size 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 = std::snprintf(nullptr, 0, fmt, std::sqrt(2)); std::vector<char> buf(sz + 1); // note +1 for null terminator std::snprintf(&buf[0], buf.size(), fmt, std::sqrt(2));
Example
#include <cstdio>
#include <limits>
#include <cstdint>
#include <cinttypes>
int main()
{
std::printf("Strings:\n");
const char* s = "Hello";
std::printf("\t[%10s]\n\t[%-10s]\n\t[%*s]\n\t[%-10.*s]\n\t[%-*.*s]\n",
s, s, 10, s, 4, s, 10, 4, s);
std::printf("Characters:\t%c %%\n", 65);
std::printf("Integers\n");
std::printf("Decimal:\t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4, -4);
std::printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6);
std::printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
std::printf("Floating point\n");
std::printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
std::printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
std::printf("Scientific:\t%E %e\n", 1.5, 1.5);
std::printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
std::printf("Special values:\t0/0=%g 1/0=%g\n", 0.0/0.0, 1.0/0.0);
std::printf("Variable width control:\n");
std::printf("right-justified variable width: '%*c'\n", 5, 'x');
int r = std::printf("left-justified variable width : '%*c'\n", -5, 'x');
std::printf("(the last printf printed %d characters)\n", r);
// fixed-width types
std::uint32_t val = std::numeric_limits<std::uint32_t>::max();
std::printf("Largest 32-bit value is %" PRIu32 " or %#" PRIx32 "\n", val, val);
}Output:
Strings:
[ Hello]
[Hello ]
[ Hello]
[Hell ]
[Hell ]
Characters: A %
Integers
Decimal: 1 2 000003 0 +4 -4
Hexadecimal: 5 a A 0x6
Octal: 12 012 04
Floating point
Rounding: 1.500000 2 1.30000000000000004440892098500626
Padding: 01.50 1.50 1.50
Scientific: 1.500000E+00 1.500000e+00
Hexadecimal: 0x1.8p+0 0X1.8P+0
Special values: 0/0=nan 1/0=inf
Variable width control:
right-justified variable width: ' x'
left-justified variable width : 'x '
(the last printf printed 40 characters)
Largest 32-bit value is 4294967295 or 0xffffffffSee also
prints formatted wide character output to stdout, a file stream or a buffer (function) |
|
|
(C++11) | prints formatted output to stdout, a file stream or a bufferusing variable argument list (function) |
| writes a character string to a file stream (function) |
|
reads formatted input from stdin, a file stream or a buffer (function) |
|
|
(C++17) | converts an integer or floating-point value to a character sequence (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/io/c/printf