zoukankan      html  css  js  c++  java
  • sprintf_s() 、sprintf()和printf()区别和用法

    转载:https://blog.csdn.net/qq_35608277/article/details/80878802

    int sprintf_s(char *buffer,size_t sizeOfBuffer,const char *format [,argument] …);
    eg:

        char buff[256];
        sprintf_s(buff,256, "../cfg/%d_%d.png", i, j);12

    异同


    printf函数把结果输出。
    sprintf函数把结果输出到指定的字符串中。
    sprintf_s()是sprintf()的安全版本,通过指定缓冲区长度来避免sprintf()存在的溢出风险
    sprintf_s 会检查格式化字符的合法性,而sprintf只会检查其是否是空指针


    需要包含的头文件

    stdio.h

    eg

    将”test 1 2”写入数组s中

    #include<stdio.h>
    int main(int argc, char *avgv[])
    {
        char s[40];
        sprintf(s,"%s%d%c","test",1,'2');
        //第一个参数就是指向要写入的那个字符串的指针,剩下的就和printf()一样

        printf("%s%d%c","test",1,'2');
        //对保存后的字符串输出
        printf("%s",s);
        return 0;
    }123456789101112

    ref

    https://blog.csdn.net/tigernana/article/details/6916491
    https://blog.csdn.net/lijie0073237/article/details/13767519
    https://blog.csdn.net/zyazky/article/details/52180458

  • 相关阅读:
    第十二周总结
    第十一周课程总结
    2020软件工程作业02
    2020软件工程第一次作业
    2019年春季学期总结
    2019年春第四次程序设计实验报告
    2019年春第一次程序设计实验报告
    2019年春第三次程序设计实验报告
    第二次课程设计实验报告
    第十二周作业
  • 原文地址:https://www.cnblogs.com/MCSFX/p/12655565.html
Copyright © 2011-2022 走看看