printf系列和vprintf系列库函数详解

先从库里面把函数模型弄出来看看:

#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> 

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

The functions in the printf() family produce output according to a format as described below. The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream; sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.

The functions snprintf() and vsnprintf() write at most size bytes (including the trailing null byte ('\0')) to str.//写入到str中的大小不能超过size,应该考虑到字符串以'\0'结尾。

The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() are equivalent to the functions printf(), fprintf(), 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. 

These eight functions write the output under the control of a format string that specifies how subsequent arguments (or arguments accessed via the variable-length argument facili-ties of stdarg(3)) are converted for output.

C99 and POSIX.1-2001 specify that the results are undefined if a call to sprintf(), snprintf(), vsprintf(), or vsnprintf() would cause to copying to take place between objects that overlap (e.g., if the target string array and one of the supplied input arguments refer to the same buffer).

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 truncated due to this limit then the return value is the number of characters (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.

**Format of the format string**  

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specifica-tion is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.


1、printf()这个基本上都知道怎么用,const char *format 将可变个参数(...)按照format格式化成字符串然后输出到标准输出流stdout。vprintf():printf的功能就是用它来实现的,所不同的是,它用一个参数取代了变长参数表,且此参数是通过调用va_start宏进行初始化,功能是送格式化输出到stdout中。

2 、fprintf 和 vfprintf 函数 把 输出内容 写到 给定的 stream 流; 

3、sprintf, snprintf, vsprintf 和 vsnprintf 函数把输出内容存放到字符串 str 中。

格式字符串 (format 参数) 由 零到多个 指令 组成: 普通字符 (除 % 外), 它们 被 原封不动的 送到 输出流; 以及 格式转换说明 (conversion specification), 每个 格式转换说明 都会 从后面 提取 零到多个 参数. 格式转换说明 由 % 字符 引导开始. 参数 必须 正确的 对应到 格式转换符 (conversion specifier) 上. 下述 字符 按顺序 列在 % 后面:。若参数指定时,可以使用上面4个函数,若是想参数列表不定,就使用下面4个函数。

example:(从别处找的)实现简易日志打印

#include <stdio.h>

#include <stdlib.h> 

#include <stdarg.h>

void FwriteLog(const char *FileName, int Line, const char *format, ...)

{  

va_list ap;/* 定义保存函数参数的结构 */  

FILE *fp;  

int retval;  

fp = NULL;  

if((fp = fopen("abc.log", "a")) == NULL) { return; }  

fprintf(fp, "源文件[%s]第[%d]行-", FileName, Line);  

va_start(ap, format); /* ap指向传入的第一个可选参数,format是最后一个确定的参数 */  

retval = vfprintf(fp, format, ap); /* 使用va_list类型参数 */  

va_end(ap);  

fprintf(fp, "%c", '\n'); fclose(fp); fp = NULL; return;

}

上面的函数这样使用:

FwriteLog(__FILE__,__LINE__, "error_code:%d, details:%s", 123, "some error.")


至于va_list的具体用法,可以参考:http://blog.csdn.net/aihao1984/article/details/5953668


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值