int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...);

函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n 的话,将不会溢出。

函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。

Result1(推荐的用法)

#include <stdio.h>
#include <stdlib.h>

int main()
{
     char str[10]={0,};
     snprintf(str, sizeof(str), "0123456789012345678");
     printf("str=%s/n", str);
     return 0;
}

root] /root/lindatest
$ ./test
str=012345678

Result2:(不推荐使用)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[10]={0, };
    snprintf(str, 18, "0123456789012345678");
    printf("str=%s/n", str);
    return 0;
}

root] /root/lindatest
$ ./test
str=01234567890123456

snprintf函数返回值的测试:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str1[10] ={0, };
    char str2[10] ={0, };
    int ret1=0,ret2=0;
    ret1=snprintf(str1, sizeof(str1), "%s", "abc");
    ret2=snprintf(str2, 4, "%s", "aaabbbccc");
    printf("aaabbbccc length=%d/n", strlen("aaabbbccc"));
    printf("str1=%s,ret1=%d/n", str1, ret1);
    printf("str2=%s,ret2=%d/n", str2, ret2);
    return 0;
}

[root] /root/lindatest
$ ./test
aaabbbccc length=9
str1=abc,ret1=3
str2=aaa,ret2=9

解释SIZE:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char dst1[10] ={0, },dst2[10] ={0, };
char src1[10] ="aaa",src2[15] ="aaabbbcccddd";
int size=sizeof(dst1);
int ret1=0, ret2=0;
ret1=snprintf(dst1, size, "str :%s", src1);
ret2=snprintf(dst2, size, "str :%s", src2);
printf("sizeof(dst1)=%d, src1=%s, /"str :%%s/"=%s%s, dst1=%s, ret1=%d/n", sizeof(dst1), src1, "str :", src1, dst1, ret1);
printf("sizeof(dst2)=%d, src2=%s, /"str :%%s/"=%s%s, dst2=%s, ret2=%d/n", sizeof(dst2), src2, "str :", src2, dst2, ret2);
return 0;
}
root] /root/lindatest
$ ./test
sizeof(dst1)=10, src1=aaa, "str :%s"=str :aaa, dst1=str :aaa, ret1=8
sizeof(dst2)=10, src2=aaabbbcccddd, "str :%s"=str :aaabbbcccddd, dst2=str :aaab, ret2=17

补充一下,snprintf的返回值是欲写入的字符串长度,而不是实际写入的字符串度。如:
char test[8];
int ret = snprintf(test,5,"1234567890");
printf("%d|%s/n",ret,test);

运行结果为:
10|1234

linux 之 snprintf函数用法的更多相关文章

  1. Linux下 fcntl 函数用法说明

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数         fcntl -- file control LIBRARY         Standard C Library (libc, ...

  2. Linux下Kill函数用法

    http://www.cnblogs.com/winnxm/archive/2010/01/22/1654502.html 用于向任何进程组或进程发送信号. #include <sys/type ...

  3. Linux C 字符串函数 sprintf()、snprintf() 详解

    一.sprintf() 函数详解 在将各种类 型的数据构造成字符串时,sprintf 的强大功能很少会让你失望. 由于 sprintf 跟 printf 在用法上几乎一样,只是打印的目的地不同而已,前 ...

  4. linux c语言 select函数用法

    linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...

  5. c++ 网络编程(五) LINUX下 socket编程 多种I/O函数 -以及readv和writev函数用法

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614056.html 一.多种I/O函数 前言:之前我们讲的数据传输一般Linux上用writ ...

  6. [转载]Linux C 字符串函数 sprintf()、snprintf() 详解

    一.sprintf() 函数详解 在将各种类 型的数据构造成字符串时,sprintf 的强大功能很少会让你失望. 由于 sprintf 跟 printf 在用法上几乎一样,只是打印的目的地不同而已,前 ...

  7. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  8. 转:对于linux下system()函数的深度理解(整理)

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  9. (笔记)Linux下system()函数的深度理解(整理)

    注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...

随机推荐

  1. 利用BBED恢复UPDATE改动前的值

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/30615151 实验步骤例如以下: 1.创建表guo_test1 gyj@PROD> ...

  2. Code Forces 414B 很不错的双手,以促进合规

    http://codeforces.com/problemset/problem/414/B 题目挺不错的.留个纪念,活动脑筋不错的题目 #include<iostream> #inclu ...

  3. Advance Installer安装问题

    一,在Advance Installer中注冊dll 1,首先将文件加入到Files And Folders中.此处以InstallValidate.dll为例. 2,在Custom Action处进 ...

  4. 几款web版整合(QQ、msn、icq、yahoo通、Gtalk等)即时聊天http://hi.baidu.com/ejie/blog/item/e89794ef9a9431efce1b3ebd.html

        直到近期为止,我们经常使用的即时聊天工具(QQ.msn等)了Web版,大家不用下载庞大软件,直接打开网页就能够与自己的好友聊天,很方便.在此将时汇总,便于大家查找,节约大家一点时间. 此都是官 ...

  5. PHP进口Excel至MySQL方法

    PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader 注意点: reader.php 中的以下这行要改动  1.将 ...

  6. Forms身份验证和基于Role的权限验证

    Forms身份验证和基于Role的权限验证 从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Memb ...

  7. DateTime.ToString("dd/MM/yyyy");后,不能直接Convert.ToDateTime的解决:

    原文:DateTime.ToString("dd/MM/yyyy");后,不能直接Convert.ToDateTime的解决: DateTime.ToString("dd ...

  8. Asp.Net MVC5入门学习系列①

    原文:Asp.Net MVC5入门学习系列① 现在直接开始MVC5的学习系列,学习资源来自Micrsoft. 开始使用Asp.Net MVC 5 打开Visual Studio 2013,然后新建一个 ...

  9. C语言库函数大全及应用实例十三

    原文:C语言库函数大全及应用实例十三                                          [编程资料]C语言库函数大全及应用实例十三 函数名: stat 功 能: 读取打 ...

  10. Installshield设置feature为必须选中状态,即必定安装状态

    原文:Installshield设置feature为必须选中状态,即必定安装状态 上一篇: 解决卸载时残留目标文件夹的问题Installation Designer --> Organizati ...