或者换句话说:可能错误printf/ fprintf十进制整数(%d,%u,%ld,%lld)格式化字符串导致程序崩溃或导致未定义的行为?
Cosinder以下代码行:
#include <iostream>
#include <cstdio>
int main() {
std::cout << sizeof(int) << std::endl
<< sizeof(long) << std::endl;
long a = 10;
long b = 20;
std::printf("%d, %d\n", a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
32位架构的结果:
4
4
10, 20
Run Code Online (Sandbox Code Playgroud)
64位架构的结果:
4
8
10, 20
Run Code Online (Sandbox Code Playgroud)
在任何情况下,程序都会打印出预期的结果.我知道,如果long值超出int范围,程序会打印错误的数字 - 这很难看,但不影响程序的主要目的 - 但除此之外,是否会发生意外情况?
c++ ×1