printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s
Header: <stdio.h>
Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks/streams:
# Declarations
int printf( const char* format, ... );
(until C99)
int printf( const char* restrict format, ... );
(since C99)
int fprintf( FILE* stream, const char* format, ... );
(until C99)
int fprintf( FILE* restrict stream, const char* restrict format, ... );
(since C99)
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, size_t bufsz,
const char* restrict format, ... );
(since C99)
int printf_s( const char* restrict format, ... );
(since C11)
int fprintf_s( FILE* restrict stream, const char* restrict format, ... );
(since C11)
int sprintf_s( char* restrict buffer, rsize_t bufsz,
const char* restrict format, ... );
(since C11)
int snprintf_s( char* restrict buffer, rsize_t bufsz,
const char* restrict format, ... );
(since C11)
# Parameters
stream: output file stream to write tobuffer: pointer to a character string to write tobufsz: up to bufsz - 1 characters may be written, plus the null terminatorformat: pointer to a null-terminated byte string specifying how to interpret the data...: arguments specifying data to print. If any argument after default argument promotions is not the type expected by the corresponding conversion specification (the expected type is the promoted type or a compatible type of the promoted type), 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.
# 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:
POSIX specifies that errno is set on error. It also specifies additional conversion specifications, most notably support for argument reordering (n$ immediately after % indicates nth argument).
Calling snprintf with zero bufsz and null pointer for buffer is useful to determine the necessary buffer size to contain the output:
snprintf_s, just like snprintf, but unlike sprintf_s, will truncate the output to fit in bufsz - 1.
# Example
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main(void)
{
const char* s = "Hello";
printf("Strings:\n"); // same as puts("Strings");
printf(" padding:\n");
printf("\t[%10s]\n", s);
printf("\t[%-10s]\n", s);
printf("\t[%*s]\n", 10, s);
printf(" truncating:\n");
printf("\t%.4s\n", s);
printf("\t%.*s\n", 3, s);
printf("Characters:\t%c %%\n", 'A');
printf("Integers:\n");
printf("\tDecimal:\t%i %d %.6i %i %.0i %+i %i\n",
1, 2, 3, 0, 0, 4,-4);
printf("\tHexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6);
printf("\tOctal:\t\t%o %#o %#o\n", 10, 10, 4);
printf("Floating-point:\n");
printf("\tRounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
printf("\tPadding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
printf("\tScientific:\t%E %e\n", 1.5, 1.5);
printf("\tHexadecimal:\t%a %A\n", 1.5, 1.5);
printf("\tSpecial values:\t0/0=%g 1/0=%g\n", 0.0 / 0.0, 1.0 / 0.0);
printf("Fixed-width types:\n");
printf("\tLargest 32-bit value is %" PRIu32 " or %#" PRIx32 "\n",
UINT32_MAX, UINT32_MAX );
}