參考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html
總覽 (SYNOPSIS)
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdarg.h>------------現在好像都在stdio.h中進行聲明。
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
描述 (DESCRIPTION)
print系列函數根據format 參數生成輸出內容。
printf和vprintf函數把輸出內容寫到stdout,即標准輸出流;
fprintf和vfprintf函數把輸出內容寫到給定的stream流;
sprintf、snprintf、 vsprintf和vsnprintf函數把輸出內容存放到字符串str中.。
這些函數由格式字符串format參數控制輸出內容,它指出怎麼樣把後面的參數(或通過stdarg(3)的變長參數機制訪問的 參數)轉換成輸出內容。
這些函數返回打印的字符數量(不包括字符串結尾用的‘\0‘)。snprintf和vsnprintf的輸出不會超過size 字節(包括了結尾的`\0'), 如果因為這個限制導致輸出內容被截斷, 則函數返回-1。(這句話解釋有誤,可以參考man手冊,原文關於返回值表述如下:)
Return value
Upon successful return, these functions return the number of characters
printed (not including the trailing ’\0’ used to end output to
strings). The functions snprintf() and vsnprintf() do not write more
than size bytes (including the trailing ’\0’). If the output was trun-
cated due to this limit then the return value is the number of charac-
ters (not including the trailing ’\0’) which would have been written to
the final string if enough space had been available. Thus, a return
value of size or more means that the output was truncated. (See also
below under NOTES.) If an output error is encountered, a negative
value is returned.
從手冊中可以看出,意思是如果輸出由於受到size的限制而被截斷輸出,返回值則是被截斷後並寫入str中的字符數(不包括‘\0’),並且前提是str有足夠的可用空間。
而如果輸出遇到錯誤,會返回一個負值。這句話是針對上面8個函數而言的。