【问题标题】:What is wrong with this C function? (printf() with getchar())这个 C 函数有什么问题? (printf() 和 getchar())
【发布时间】:2016-11-12 18:50:51
【问题描述】:
printf( "%3o\t%2x\t%3d\t%c\n", c = getchar(), c, c, c );

我收到一条警告,上面写着“未排序的修改和访问 'c' [-Wunsequenced]”。该错误很容易修复;我所要做的就是将getchar()printf() 分开,但我只是想更好地理解为什么这条指令会产生警告,以及如果我实际运行它会出现什么问题。

这与使用可变长度参数列表的 CPP 宏实现 printf() 有什么关系吗?

【问题讨论】:

标签: c printf warnings getchar


【解决方案1】:

The order in which arguments passed to a function call are evaluated is unspecified。无法保证在读取最后三个 c 参数之前将 getchar() 的结果分配给 c

在函数调用之外调用 c = getchar() 可以解决此问题。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2014-06-29
    • 2011-02-13
    • 2011-04-24
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多