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

snprintf()返回值的陷阱的更多相关文章

  1. snprintf 返回值陷阱 重新封装

    snprintf()函数用于将格式化的数据写入字符串,其原型为: int snprintf(char *str, int n, char * format [, argument, ...]); st ...

  2. snprintf 返回值

    在平时写代码的过程中,我一个推荐带有n系列的字符串函数,如 strcat ->strncat sprintf->snprintf 我们有类似的一个函数 void dump_kid(std: ...

  3. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bashexit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.sy ...

  4. C#:struct的陷阱:无法修改“xxx”的返回值,因为它不是变量

    示例代码如下: public struct SpiderResult { public string robotName; public string RobotName { get { return ...

  5. 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用

    来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...

  6. Java 中无参带返回值方法的使用

    如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...

  7. Python函数的定义与调用、返回值、参数

    一.函数是什么 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.比如print(),len()等.但你也可以自己创建函数,这被叫做用户自 ...

  8. Python_初识函数和返回值_22

    #len s = '金老板小护士' len(s) def my_len(): #自定义函数 i = 0 for k in s: i += 1 print(i) length = my_len() pr ...

  9. 总结day7 ---- 函数的内容 ,初识,返回值,进阶(一)

    内容大纲: 一: 函数识别 二: 函数的结构 三: 函数的返回值, 四: 函数的参数 五: 动态参数 六: 形参的顺序 七: 名称空间 八: 作用域 九: 加载顺序和取值顺序 十: 内置函数 十一: ...

随机推荐

  1. swift4.0 方法监听Selector写法总结

    import UIKit class MainViewController: UITabBarController { //MARK:属性 懒加载 lazy var composeBtn = UIBu ...

  2. HihoCoder 1121二分图一•二分图判定

    背景: 个名字,表示这两个人有一场相亲.由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别.因此她拜托我检查一下相亲表里面有没有错误的记录,即是否把两个同性安排 ...

  3. 小程序地区时间自定义选择器 picker

    进入微信公众平台小程序开发文档搜索 picker 点进去后下滑,点击在开发者工具中预览即可

  4. c# 中的 protected internal 如何在 vc.net 中实现

    c# 中有 protected internal 的复合访问属性, 保证assembly内部访问,以及外部的派生类访问 vc.net 中无法直接写上 protected internal, 其对应的写 ...

  5. CF364D Ghd(随机化)

    另一个集合\(s\)的\(ghd\)为\(max\{gcd(s')||s'|>=0.5|s|\}\) 给定序列\(a\),求\(ghd\) 随机化算法.因为\(|s'|\geq 0.5|S|\) ...

  6. iOS7 UITableView Row Height Estimation

    This post is part of a daily series of posts introducing the most exciting new parts of iOS7 for dev ...

  7. Luogu P1514引水入城【搜索】 By cellur925

    题目传送门 这道题开始看好像并没有什么思路,和搜索好像也并没有什么关系.但是我们手玩下样例就会发现,思路其实就三句话:(写这道题的时候在代码里写的) //我们想知道从第1行的每列往下到干旱区的范围 / ...

  8. Unrecogized font family ‘Ionicons’ 在ios上报错,android正常

    解决方法: react-native link react-native-vector-icons 很多模块都需要link一下

  9. random 库

    random 是使用随机数的python 标准库 ——为随机数:采用梅森旋转算法生成的(伪)随机序列中的元素 —— import random 基本随机数函数:seed(),random() 扩展随机 ...

  10. aws cli command line interface的安装与使用

    安装 在centos中安装aws cli,安装依赖python,先装好python,然后按下述命令执行 yum install wget wget https://bootstrap.pypa.io/ ...