首页 > 解决方案 > 由于某种原因“printf_s”产生错误:“未在此范围内声明”

问题描述

我下载了 MinGW-64 并运行了这段代码(缩短了,因为它最初是一个堆栈、队列和其他 DS 实现):

#include <iostream>
#include <cstdio>

int main()
{
    const int _sizeS = 20;
    .
    .
    .
    int _tmp;
    printf_s("\nEnter an alpha-numeric character to quit: ");
    
    std::cin >> _tmp;
    return 0;
}

这在 VS2019 中完美编译,并且可以满足我的要求。

但是,我g++ pyrmd.cpp在命令行中使用并得到一个错误:

pyrmd.cpp: In function 'int main()':
pyrmd.cpp:6:2: error: 'printf_s' was not declared in this scope; did you mean 'printf'?
    6 |  printf_s("\n Enter a character: \n");
      |  ^~~~~~~~
      |  printf

有没有人知道为什么会发生这种情况,以及如何解决它?

标签: c++compiler-errors

解决方案


弄清楚自己并从评论中确认问题出在 GCC 本身。

GCC的包含库没有 printf_s 的声明。头文件stdio.hcstdio缺少声明。然而,VS2019 具有提供这些_s功能的 MSVC 编译器。


推荐阅读