【问题标题】:Why scanf_s does not work anywhere other than visual studio? [duplicate]为什么 scanf_s 不能在 Visual Studio 以外的任何地方工作? [复制]
【发布时间】:2019-06-01 07:06:35
【问题描述】:

当我使用 Visual Studio 时,它说“scanf 不安全,请尝试使用 scanf_s”。但是如果我在 gcc 或其他编译器中使用 scanf_s,它就不起作用。 scanf_s 是否仅适用于 Visual Studio?如果是这样,为什么?视觉工作室网站说“scanf 不安全”。如果它不安全,为什么其他人仍然使用它?

【问题讨论】:

    标签: c visual-studio gcc


    【解决方案1】:

    作为mentioned here

    scanf_s 是特定于 Microsoft 的。标头是 stdio.h 但不在 GCC 中。

    作为documented here

    读取带有scanf 的字符串时,始终指定%s 格式的宽度(例如,“%32s”而不是“%s”);否则,格式不正确的输入很容易导致缓冲区溢出。
    或者,考虑使用scanf_s_scanf_s_lwscanf_s_wscanf_s_lfgets

    在“Why didn't gcc implement _s functions?”查看更多信息

    pmg 在 cmets 中添加 scanf_s()Standard C11 (optional)
    这意味着activating c11 with gcc 可能就足够了。

    不过Shawn 补充道:

    IIRC,微软的版本不符合标准。
    此外,没有其他主要的 C 库供应商费心实施 Annex K,因此它可能是针对所有意图和目的的特定于 MS 的。

    pmg 确认:

    我的gcc(6.3.0 版)无法识别scanf_s()gcc -std=c11 -pedantic ...

    【讨论】:

    • scanf_s() 是标准 C11 (optional)
    • @pmg IIRC,微软的版本不符合标准。此外,没有其他主要的 C 库供应商费心实施 Annex K,因此它可能是针对所有意图和目的的特定于 MS 的。
    • @pmg 那么用 gcc 激活 c11 就足够了吗? (stackoverflow.com/questions/16256586/…)
    • 我的 gcc(6.3.0 版)无法识别 scanf_s()gcc -std=c11 -pedantic ...
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2022-01-16
    • 2019-12-16
    相关资源
    最近更新 更多