日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sprintf_s的使用

發布時間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sprintf_s的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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_s,?fprintf_s,?sprintf_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[20];sprintf_s(tempArray, 20, "Int %d", 1); #endif

?Windows的中用法如下:

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

?

轉載于:https://www.cnblogs.com/dirt2/p/6104198.html

總結

以上是生活随笔為你收集整理的sprintf_s的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。