参考: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个函数而言的。

printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数的更多相关文章

  1. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换

    总览 (SYNOPSIS) #include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream ...

  2. printf()、sprintf()、vprintf()、vsprintf()(转)

    转自http://sumsung753.blog.163.com/blog/static/14636450120112151092934/ 一.printf() printf()函数优点在于可以格式化 ...

  3. [misc]printf/fprintf/sprintf/snprintf函数

    转自:http://blog.csdn.net/To_Be_IT_1/article/details/32179549 需要包含的头文件 #include <stdio.h> int pr ...

  4. 实例-sprintf() 函数详解-输出格式转换函数

    Part1:实例 $filterfile = basename(PHP_SELF, '.php'); if (isset($_GET['uselastfilter']) && isse ...

  5. [ Skill ] print println printf fprintf sprintf lsprintf

    https://www.cnblogs.com/yeungchie/ 几种 print 函数的差异 print 接收任意的数据类型,并打印到 CIW print( 12345 ) ; 12345 pr ...

  6. printf、sprintf与fprintf 的用法区分

    原文链接 1: fprintf()#include <stdio.h> int fprintf( FILE *stream, const char *format, ... );fprin ...

  7. sprintf,snprintf的用法(可以作为linux中itoa函数的补充)【转】

    转自:http://blog.csdn.net/educast/article/details/25068445 函数功能:把格式化的数据写入某个字符串 头文件:stdio.h 函数原型:int sp ...

  8. PHP中的输出:echo、print、printf、sprintf、print_r和var_dump

    大家在面试中,经常会被问到的问题: 请简要说明PHP的打印方式都有哪些? 或者直接点问: 请说明echo.print.print_r的区别 看着很简单,一般会出现在初中级的笔试题中.但是要真正说明白这 ...

  9. php输出echo、print、print_r、printf、sprintf、var_dump的区别比较

    本篇文章是对php输出echo.print.print_r.printf.sprintf.var_dump的区别进行了详细的分析介绍,需要的朋友参考下     用.net开发已经5年了,最近突然想接触 ...

随机推荐

  1. [OFBiz]开发 二

    1.svn中check出的apache-ofbiz-10.04(svn_2010-04-01代码备分)由于它的所有文件都不含有中文,所以Eclipse使用什么编码方式都可以(ISO, GBK, UTF ...

  2. T-SQL 常用语句学习

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server ---  ...

  3. 使用Qmake在树莓派上开发Opencv程序

    Qt 安装 PC 端  下载安装即可 https://mirrors.ustc.edu.cn/qtproject/official_releases/qt 树莓派:Qt开发套件和opencv安装sud ...

  4. redo文件二

    为什么要引入LGWR后台进程和redo log buffer 如果使用前台进程来将redo的信息写入到redo日志文件组中,那么会导致并发的前台进程对redo日志文件组的争用,从而使用后台进程LGWR ...

  5. mysql操作时遇到的小问题

    mysql数据库在程序中执行sql语句时,或者在执行sql时,数据库表可能会有一些特殊的字符,比如说#,.等,这样在执行时 可能会遇到问题 如以下的表名,backup_2014.2.22, 这个表在查 ...

  6. hdfs 数据块重分布 sbin/start-balancer.sh -threshold

    数据块重分布sbin/start-balancer.sh -threshold <percentage of disk capacity>percentage of disk capa ...

  7. Hadoop中FileSystem的append方法

    今天在使用Hadoop 1.1.2版本进行FileSystem的append操作时报以下异常: org.apache.hadoop.ipc.RemoteException: java.io.IOExc ...

  8. POJ 1236 Network of Schools (有向图的强连通分量)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9073   Accepted: 359 ...

  9. 第八章、Linux 磁盘与文件系统管理

    认识 EXT2 文件系统 Linux最传统的磁盘文件系统(filesystem)使用的是EXT2这个啦!所以要了解文件系统就得要由认识EXT2开始! 而文件系统是创建在硬盘上面的,因此我们得了解硬盘的 ...

  10. InnoDB与MyISAM的区别

    MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...