c里面的printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s一问说清所有各种printf
都定义在<stdio.h>中,有些事C99的一部分,有些事C11新加的。如下:
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, size_t bufsz, const char* restrict format, ... ); |
(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, const char* restrict format, ... ); |
(7) | (since C11) |
int snprintf_s( char* restrict buffer, rsize_t bufsz, const char* restrict format, ... ); |
(8) | (since C11) |
各个gcc编译器版本对c标准的支持情况参见这里。
规则为:普通的printf就是打印,sprintf就是格式化用途、叫做messageformat更合适。fprintf就是指定流而不是使用stdout标准流。带n版本为指定长度。带s的版本为所谓的安全版。
所以简单一点,格式化就用snprintf,写文件就用fprintf。
对应printf还有wprintf,vprintf,w\v代替f。w是宽字符。v版本和非v版本的区别在于,v版本自己管理可变参数。如下:
The functions vprintf(), vfprintf(), vdprintf(), vsprintf(), vsnprintf() are equivalent to the functions printf(), fprintf(), dprintf(), sprintf(), snprintf(), respectively, except that they are called with a va_list instead of a variable number of arguments. These functions do not call the va_end macro. Because they invoke the va_arg macro, the value of ap is undefined after the call.
带_s是所谓的安全版本,c11标准的一部分,会检查参数,由宏__STDC_WANT_LIB_EXT1__控制。参见https://software-dl.ti.com/codegen/docs/tiarmclang/compiler_tools_user_guide/compiler_manual/compiler_security/C11_secure_functions.html,https://docs.oracle.com/cd/E88353_01/html/E37843/snprintf-s-3c.html
参考:https://en.cppreference.com/w/c/io/fprintf
https://www.man7.org/linux/man-pages/man3/vsnprintf.3.html