相关疑难解决方法(0)

如果使用错误的格式字符串调用printf会发生什么?

或者换句话说:可能错误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++

4
推荐指数
1
解决办法
2344
查看次数

将整数打印为浮点数

可能重复:
打印int作为float时printf的行为是什么?

int main()
{
 int x=4;
 int y=987634;
 printf("%f %f",x,y);
}
Run Code Online (Sandbox Code Playgroud)

在编译此代码时,我得到一个0.000000 0.000000的输出.难道不存在x和y到浮点数的类型提升吗?O/P不应该是4.000000和987634.000000吗?谁能帮我这个.Thanx提前.

c floating-point

2
推荐指数
1
解决办法
102
查看次数

标签 统计

c ×1

c++ ×1

floating-point ×1