sprintf用法解析

int sprintf ( char * str, const char * format, ... );

描述:

将格式化的数据写入字符串
将内容作为C字符串存储在str指向的缓冲区中,不会打印。
缓冲区的大小应该足够大以包含整个结果字符串
内容后自动添加一个终止空字符。
在格式参数之后,函数至少需要格式所需的其他参数。

参数:

str

指向存储结果C字符串的缓冲区的指针。
缓冲区应该足够大以包含结果字符串。

format

包含格式字符串的C字符串,其格式字符串与printf中的格式相同

返回值:

成功时,返回写入的字符总数。 此计数不包括自动附加在字符串末尾的附加空字符。
失败时,返回一个负数。

扫描二维码关注公众号,回复: 13248841 查看本文章
/* sprintf example */
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}
Output:

[5 plus 3 is 8] is a string 13 chars long


猜你喜欢

转载自blog.csdn.net/swif_N_F/article/details/78688145
今日推荐