scanf int8_t 损坏堆栈
scanf int8_t corrupts stack
如何扫描 int8_t 和其他类型而不出现此错误。我使用 "cinttypes" 来获取模式常量,但这没有帮助。
#include <cstdio>
#include <cstdint>
#include <cinttypes>
int main()
{
int8_t var;
scanf("%" SCNi8, &var);
printf("%" PRIi8 "\n", var);
return 0;
}
P.S。这个错误只在Debug中出现,在Release中构建时就OK了。
P.P.S。输出为:
1>------ Build started: Project: SCANF_PROBLEM, Configuration: Debug Win32 ------
1> SCANF_PROBLEM.cpp
1>d:\study\scanf_problem\scanf_problem\scanf_problem.cpp(11): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\visual studio\vc\include\stdio.h(283) : see declaration of 'scanf'
1> SCANF_PROBLEM.vcxproj -> D:\Study\SCANF_PROBLEM\Debug\SCANF_PROBLEM.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
即使在 C99 中,也不需要存在这些扩展整数类型,除非文档中说它们存在,稍后会详细介绍...即使这些扩展整数类型确实存在,它们也不需要 scanf
格式说明符。请参阅 n1256 section 7.8.1p6 以获取此引述:
For each type that the implementation provides in <stdint.h>
, the corresponding
fprintf
macros shall be defined and the corresponding fscanf
macros shall be
defined unless the implementation does not have a suitable fscanf
length modifier for
the type.
至于 C++,嗯...C++11 委托 <cstdint>
本质上应该是 C99 的包装器 <stdint.h>
,它所引用的函数是 C99 等价物的包装器。
关键在于:Microsoft 从来没有那么关心 C99,例如they're willing to document "N/A" for extended integer support。
确保您的编译器配置为编译为 C99 或更高版本,或者 C++11 或更高版本,并且 link 为 C99-or-later-compliant 标准库。 SCNi8
可能存在于 headers 中,但如果 scanf
不支持它(它不会,在 C99/C++11 合规之前)那么你不会运气不好...当然,确保您的标准库文档支持您打算使用的 implementation-defined 功能。
提示:如果您的文件名以 .c 结尾,Microsoft Visual Studio 将尝试将您的代码编译为 C89。这可能不会有帮助。 MSVC++ 在标准库方面的 C99 兼容性不是很好,即使您告诉它编译为 C++11。在学习如何使用命令行进行编译时,您可能应该学习如何确定正在使用的 compiler/library 版本。
提示 #2:您可以使用 LLVM/Clang in Visual Studio。确保你 link 使用符合 C99 的标准库(例如,不是 Microsoft)。
提示 #3:当使用 printf
...
时,您可能希望将 SCNi8
更改为 PRIi8
提示 #4:请参阅 footnote 191 以了解此引述:
C++ implementations should define these macros only when __STDC_FORMAT_MACROS
is defined
before <inttypes.h>
is included.
您 运行 进入了 Microsoft C/C++ 运行time 库的错误,请参见。 http://mfctips.com/tag/format/ or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417(它报告了 mingw 下 gcc 的这个错误,它链接到 Microsft 库)。
"%hhd" 根本行不通;你必须围绕它进行编程(这并不难,但很难过)。
我不知道向 MS 报告错误(即,我在尝试 google 时没有看到任何内容)。
如何扫描 int8_t 和其他类型而不出现此错误。我使用 "cinttypes" 来获取模式常量,但这没有帮助。
#include <cstdio>
#include <cstdint>
#include <cinttypes>
int main()
{
int8_t var;
scanf("%" SCNi8, &var);
printf("%" PRIi8 "\n", var);
return 0;
}
P.S。这个错误只在Debug中出现,在Release中构建时就OK了。
P.P.S。输出为:
1>------ Build started: Project: SCANF_PROBLEM, Configuration: Debug Win32 ------
1> SCANF_PROBLEM.cpp
1>d:\study\scanf_problem\scanf_problem\scanf_problem.cpp(11): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\visual studio\vc\include\stdio.h(283) : see declaration of 'scanf'
1> SCANF_PROBLEM.vcxproj -> D:\Study\SCANF_PROBLEM\Debug\SCANF_PROBLEM.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
即使在 C99 中,也不需要存在这些扩展整数类型,除非文档中说它们存在,稍后会详细介绍...即使这些扩展整数类型确实存在,它们也不需要 scanf
格式说明符。请参阅 n1256 section 7.8.1p6 以获取此引述:
For each type that the implementation provides in
<stdint.h>
, the correspondingfprintf
macros shall be defined and the correspondingfscanf
macros shall be defined unless the implementation does not have a suitablefscanf
length modifier for the type.
至于 C++,嗯...C++11 委托 <cstdint>
本质上应该是 C99 的包装器 <stdint.h>
,它所引用的函数是 C99 等价物的包装器。
关键在于:Microsoft 从来没有那么关心 C99,例如they're willing to document "N/A" for extended integer support。
确保您的编译器配置为编译为 C99 或更高版本,或者 C++11 或更高版本,并且 link 为 C99-or-later-compliant 标准库。 SCNi8
可能存在于 headers 中,但如果 scanf
不支持它(它不会,在 C99/C++11 合规之前)那么你不会运气不好...当然,确保您的标准库文档支持您打算使用的 implementation-defined 功能。
提示:如果您的文件名以 .c 结尾,Microsoft Visual Studio 将尝试将您的代码编译为 C89。这可能不会有帮助。 MSVC++ 在标准库方面的 C99 兼容性不是很好,即使您告诉它编译为 C++11。在学习如何使用命令行进行编译时,您可能应该学习如何确定正在使用的 compiler/library 版本。
提示 #2:您可以使用 LLVM/Clang in Visual Studio。确保你 link 使用符合 C99 的标准库(例如,不是 Microsoft)。
提示 #3:当使用 printf
...
SCNi8
更改为 PRIi8
提示 #4:请参阅 footnote 191 以了解此引述:
C++ implementations should define these macros only when
__STDC_FORMAT_MACROS
is defined before<inttypes.h>
is included.
您 运行 进入了 Microsoft C/C++ 运行time 库的错误,请参见。 http://mfctips.com/tag/format/ or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417(它报告了 mingw 下 gcc 的这个错误,它链接到 Microsft 库)。
"%hhd" 根本行不通;你必须围绕它进行编程(这并不难,但很难过)。
我不知道向 MS 报告错误(即,我在尝试 google 时没有看到任何内容)。