sprintf_s was not declared in this scope

前端 未结 6 1227
悲&欢浪女
悲&欢浪女 2021-01-01 10:28

I have a C program that uses sprintf_s. It works fine in Windows, but when I compile my code in Linux it gives this error:

sprintf_s was not dec         


        
6条回答
  •  旧巷少年郎
    2021-01-01 11:19

    sprintf_s is only part of Annex K, an optional Annex to the C11 standard:

    Annex K

    ...

    K.2 Scope

    1. This annex specifies a series of optional extensions that can be useful in the mitigation of security vulnerabilities in programs, and comprise new functions, macros, and types declared or defined in existing standard headers.

    ...

    K.3.5.3.6 The sprintf_s function

    Synopsis

    #define __STDC_WANT_LIB_EXT1__1
    #include 
    int sprintf_s(char * restrict s, rsize_t n,
    const char * restrict format, ...);
    

    (emphasis added)

    It never made it into POSIX (or Linux) (and is not missed at all, there are even arguments about its usefulness in the committee).

    For better portability, use snprintf which is part of the core standard and provides all the functionality you'll need.

提交回复
热议问题