snprintf拷贝字符串

函数原型:int snprintf(char *dest, size_t n, const char *fmt, ...);
函数说明: 最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n的话,将不会溢出。
函数返回值: 若成功则返回存入数组的字符数,若编码出错则返回负值。

#include <stdio.h>
#include <stdlib.h>
int main()
{
     char str[10]={0,};
     snprintf(str, sizeof(str), "0123456789012345678");
     printf("str=%s/n", str);
     return 0;
}

posted on 2013-10-31 14:02  小尾巴猴子  阅读(298)  评论(0编辑  收藏  举报

导航