int sprintf_s(char *restrict buffer, rsize_t bufsz,
              const char *restrict format, ...);

将数据格式化输出到字符串,sprintf_s()是sprintf()的安全版本,通过指定缓冲区长度来避免sprintf()存在的溢出风险。

sprintf_s原先只有windows的编译器才只支持,并不是C中的标准函数。

在C11标准中加入了对该函数的支持,但是是可选的,并非强制加入。

C11中规定,如果编译器实现了__STDC_LIB_EXT1__ 宏,那么就要支持对该函数的实现。

gcc编译器只是部分的支持C11标准,本人测试在ubuntu的gcc 5.4.0版本中也没有实现__STDC_LIB_EXT1__ 。

gcc中可以用snprintf函数简单替代sprintf_s,但是注意2者在实现上是有一定的区别,不是完全相同。

int snprintf( char *restrict buffer, int bufsz, 
              const char *restrict format, ... );

C11原文如下:

__STDC_LIB_EXT1__ The integer constant 201ymmL, intended to indicate support
for the extensions defined in annex K (Bounds-checking interfaces).
Implementations that do not define __STDC_LIB_EXT1__ are not required to conform to these
specifications.

C++网站给出的使用建议如下:

As all bounds-checked functions, printf_sfprintf_ssprintf_s, and snrintf_s

are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation

and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

参考用法如下:

#if defined(__STDC_LIB_EXT1__)
#if (__STDC_LIB_EXT1__ >= 201112L)
#define __STDC_WANT_LIB_EXT1__ 1 /* Want the ext1 functions */
#endif
#endif #include <stdlib.h>
#include <string.h>
#include <stdio.h> #if (__STDC_WANT_LIB_EXT1__ == 1)
char tempArray[];
sprintf_s(tempArray, , "Int %d", );
#endif

Windows的中用法如下:

#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
sprintf_s(...)
#else
sprintf(...)
#endif

sprintf_s的使用的更多相关文章

  1. sprintf_s的教训

    sprintf_s 是个比sprintf更安全的函数,今天在使用的过程中犯了个错误,代码的大致意思如下 void Test_sprintf_s() { ]; memset(buff, , sizeof ...

  2. VS2013编译报错error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

    解决方法有两个: 1. 在预编译头文件stdafx.h里(在没有include任何头文件之前)定义下面的宏: #define _CRT_SECURE_NO_DEPRECATE 2. 将sprintf函 ...

  3. sprinf sprintf_s 的用法

    函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...

  4. Qt sprintf_s函数格式化字符串出错

    Qt sprintf_s函数格式化字符串出错 问题的出现: 我在VS上用c C++写的跨平台的函数 移植到Qt 上面 出现sprintf_s 函数格式化出错. 开始以为是编码问题  反复查找Qt乱码问 ...

  5. linux下sprintf_s函数的替代

    error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...

  6. sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析

    看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...

  7. sprintf_s函数用法

    函数功能:将数据格式化输出到字符串 函数原型: int sprintf_s( char *buffer, size_t sizeOfBuffer, const char *format [, argu ...

  8. c++中sprintf和sprintf_s的区别

    参考:https://blog.csdn.net/qq_37221466/article/details/81140901 sprintf_s是sprintf的安全版本,指定缓冲区长度来避免sprin ...

  9. sprintf_s() 、sprintf()和printf()区别和用法

    转载:https://blog.csdn.net/qq_35608277/article/details/80878802 int sprintf_s(char *buffer,size_t size ...

随机推荐

  1. mac 系统通用快捷键(mac 下的应用多数会往这些标准看齐)(转:http://yang3wei.github.io/blog/2013/02/08/chen-ni-yu-mac-chen-ni-yu-xcode/)

    command + w: 关闭当前窗口 command + q: 退出程序 (Google Chrome 有点奇葩,按下之后还需要hold 那么一小会儿才能退出) command + m: 最小化当前 ...

  2. Shape + Selector: Make a Shape as one item of the Selector

    Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...

  3. Python自动化之select解析

    select原理 网络通信被Unix系统抽象为文件的读写,通常是一个设备,由设备驱动程序提供,驱动可以知道自身的数据是否可用.支持阻塞操作的设备驱动通常会实现一组自身的等待队列,如读/写等待队列用于支 ...

  4. 浏览器js自动查表脚本

    javascript: void((function() {$.get("", {wen: "880350384879600241",action: " ...

  5. MappingJacksonHttpMessageConverter过期

    最新在做一个SSM整合学习项目,maven加上jackson库,spring配置json,tomcat不能正常部署启动 Maven配置 <!-- 引入jackson,防止ajax调用返回json ...

  6. jQuery工作原理

    jQuery的开篇声明里有一段非常重要的话:jQuery是为了改变javascript的编码方式而设计的.从这段话可以看出jQuery本身并不是UI组件库或其他的一般AJAX类库.jQuery改变ja ...

  7. 全局对象的构造函数会在main 函数之前执行

    #include <iostream> using namespace std; class A { public: A() { cout << "Generator ...

  8. Metronic 与 VS2013/2015 合作开发

    Metronic 与 VS2013/2015 合作开发  去年购买了一个:METRONIC  (http://www.keenthemes.com/) ,最近下了最新的版本:V3.7 ,解压缩后,目录 ...

  9. ios Unit test 入门书籍推荐

    请参考 ios 7 by tutorials 中的 chapter 11 :Unit Testing in xcode 5

  10. 在本地计算机无法启动MYSQL服务错误1067进程意外终止

    在本地计算机无法启动MYSQL服务错误1067进程意外终止 这种情况一般是my.ini文件配置出错了, 你可以删除系统目录下的my.ini文件, 把下面的内容重新写入my.ini文件试试, 要适当地改 ...