【依赖头文件】
#include<stdio.h>
【函数原型】
int snprintf(char *str, size_t size, const char *format, …);
【函数参数】
str:目标字符串;size:拷贝字节数(Bytes); format:源字符串; …格式
【函数功能】
最多从源字符串format中拷贝size字节的内容(含字符串结尾标志’ ’)到目标字符串
The functions snprintf() write at most size bytes (including the terminating null byte (’ ’)) to str.
【返回值】
成功返回源串的长度(strlen, 不含’ ’)
失败返回负值
【例子及理解】
由于最多拷贝size个字节到目标字符串,那么通常目标字符串就设置成size大小就不会有越界问题
下面将目标字符串的长度设置为size大小,分别实验源串长度小于,等于,大于size的情况
【情形一】:源串小于目标字符串
实际上源串为:“123 ”,所以只将字符’1’,‘2’,‘3’, ' ’拷到了目标串,返回值为源串的strlen为3
1 #include <stdio.h> 2 #include <strings.h> 3 #include <string.h> 4 int main(void) 5 { 6 char a[10] = {' '}; 7 int i = 0; 8 int ret = 0; 9 memset(a, 1, sizeof(a)); 10 for(i = 0; i < 10; i++) 11 { 12 printf("a[%d] = %dn",i,a[i]); 13 } 14 15 ret = snprintf(a, 10, "%d", 123); 16 printf("ret = %dn",ret); 17 18 for(i = 0; i < 10; i++) 19 { 20 printf("a[%d] = %dn",i,a[i]); 21 } 22 return 0; 23 } root@ubuntu:/shiyan/shiyan265# gcc snprintf.c root@ubuntu:/shiyan/shiyan265# ./a.out a[0] = 1a[1] = 1a[2] = 1a[3] = 1a[4] = 1a[5] = 1a[6] = 1a[7] = 1a[8] = 1a[9] = 1ret = 3a[0] = 49a[1] = 50a[2] = 51a[3] = 0a[4] = 1a[5] = 1a[6] = 1a[7] = 1a[8] = 1a[9] = 1root@ubuntu:/shiyan/shiyan265#【情形二】:源串等于目标字符串
源串为"abcdefghi ",正好将源串拷贝到目标字符串中,返回值为源串的strlen为9
1 #include <stdio.h>2 #include <strings.h>3 #include <string.h>4 int main(void)5 { 6 char a[10] = {' '};7 int i = 0;8 int ret = 0;9 memset(a, 1, sizeof(a));10 for(i = 0; i < 10; i++)11 { 12 printf("a[%d] = %dn",i,a[i]);13 }14 15 //ret = snprintf(a, 10, "%d", 123);16 ret = snprintf(a, 10, "%s","abcdefghi");17 printf("ret = %dn",ret);18 19 for(i = 0; i < 10; i++)20 { 21 printf("a[%d] = %dn",i,a[i]);22 }23 return 0; root@ubuntu:/shiyan/shiyan265# gcc snprintf.c root@ubuntu:/shiyan/shiyan265# ./a.out a[0] = 1a[1] = 1a[2] = 1a[3] = 1a[4] = 1a[5] = 1a[6] = 1a[7] = 1a[8] = 1a[9] = 1ret = 9a[0] = 97a[1] = 98a[2] = 99a[3] = 100a[4] = 101a[5] = 102a[6] = 103a[7] = 104a[8] = 105a[9] = 0root@ubuntu:/shiyan/shiyan265#【情形三】源串大于目标字符串
源串为"abcdefghijklmnopq ",最多从源串拷贝10个字节(含 )到目标串那么就是拷贝源串的9个字节内容(abcdefghi)再加一个 到目标串,目标串的结果和情形二一样,但是ret返回值变成了17,即strlen(“abcdefghijklmnopq”)
1 #include <stdio.h> 2 #include <strings.h> 3 #include <string.h> 4 int main(void) 5 { 6 char a[10] = {' '}; 7 int i = 0; 8 int ret = 0; 9 memset(a, 1, sizeof(a)); 10 for(i = 0; i < 10; i++) 11 { 12 printf("a[%d] = %dn",i,a[i]); 13 } 14 15 //ret = snprintf(a, 10, "%d", 123); 16 //ret = snprintf(a, 10, "%s","abcdefghi"); 17 ret = snprintf(a, 10, "%s", "abcdefghijklmnopq"); 18 printf("ret = %dn",ret); 19 20 for(i = 0; i < 10; i++) 21 { 22 printf("a[%d] = %dn",i,a[i]); 23 } 24 return 0; 25 } root@ubuntu:/shiyan/shiyan265# gcc snprintf.c root@ubuntu:/shiyan/shiyan265# ./a.out a[0] = 1a[1] = 1a[2] = 1a[3] = 1a[4] = 1a[5] = 1a[6] = 1a[7] = 1a[8] = 1a[9] = 1ret = 17a[0] = 97a[1] = 98a[2] = 99a[3] = 100a[4] = 101a[5] = 102a[6] = 103a[7] = 104a[8] = 105a[9] = 0root@ubuntu:/shiyan/shiyan265#================================================================================
函数原型:int snprintf(char* dest_str,size_t size,const char* format,…);
所需头文件:#include<stdio.h>
函数功能:先将可变参数 “…” 按照format的格式格式化为字符串,然后再将其拷贝至dest_str中。
注意事项:如果格式化后的字符串长度小于size,则将字符串全部拷贝至dest_str中,并在字符串结尾处加上‘ ’;
如果格式化后的字符串长度大于或等于size,则将字符串的(size-1)拷贝至dest_str中,然后在字符串结尾处加上’ ’.
函数返回值是 格式化字符串的长度。
实例:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ char dest_str[1024]; memset(dest_str,0,sizeof(dest_str)); char *s1 = "Linux C程序设计"; int size = strlen(s1); int year = 2017; int month = 9; int day = 8; snprintf(dest_str,sizeof(dest_str),"字符串:%sn长度是:%dn今天是:%d年%d月%d日n",s1,size,year,month,day); printf("%s",dest_str); return 0; } 执行结果字符串:Linux C程序设计长度是:15今天是:2017年9月8日参考1: https://blog.csdn.net/liaojunwu/article/details/80489406
参考2: https://blog.csdn.net/qq_34707315/article/details/77895735