一、参数及头文件(Description)
snprintf()函数用于将格式化的数据写入字符串,其原型为:
int snprintf(char *str, int n, char * format [, argument, ... ] );
头文件位于:#include <stdio.h>
Params:char *str, size_t size, const char *format [, argument, ... ]
其中str表示存放字符串的地址,size表示要写入的字符的最大数目,包括字符串结束符‘/0’.format为格式化字符串,与printf()函数相同,argument为变量,snprintf()和printf()一样,是不定参数的函数。
二、返回值 ( Return value)
-
if successful, these functions return the number of characters printed (excluding the null byte used to end output to strings)。
即如果成功,返回打印字符串的个数,不包含结束字符串打印的'/0'。
函数snprintf() 和vsnprintf() 不能写入超过第二个参数定义的size bytes(包括字符串结束符'\0')到str,如果输出因为这个限制被截断,得到的返回值是打印到str的字符串个数(不包括字符串结束符'\0')。如果有足够buffer空间可用,这个字符串会一直打印显示,直到最后字符串最后。
如果遇到输出错误,会返回一个负值(a negative value)。