这个 C 函数有什么问题? (printf() 与 getchar())
What is wrong with this C function? (printf() with getchar())
printf( "%3o\t%2x\t%3d\t%c\n", c = getchar(), c, c, c );
我收到一条警告 "unsequenced modification and access to 'c' [-Wunsequenced]"。该错误很容易修复;我所要做的就是将 getchar()
与 printf()
分开,但我只是想更好地理解为什么该指令会产生警告,以及如果我实际上 运行 会出现什么问题它。
这与 printf()
对可变长度参数列表使用 CPP 宏的实现有什么关系吗?
The order in which arguments passed to a function call are evaluated is unspecified。无法保证 getchar()
的结果会在读取最后三个 c
参数之前分配给 c
。
在函数调用之外调用 c = getchar()
解决了这个问题。
printf( "%3o\t%2x\t%3d\t%c\n", c = getchar(), c, c, c );
我收到一条警告 "unsequenced modification and access to 'c' [-Wunsequenced]"。该错误很容易修复;我所要做的就是将 getchar()
与 printf()
分开,但我只是想更好地理解为什么该指令会产生警告,以及如果我实际上 运行 会出现什么问题它。
这与 printf()
对可变长度参数列表使用 CPP 宏的实现有什么关系吗?
The order in which arguments passed to a function call are evaluated is unspecified。无法保证 getchar()
的结果会在读取最后三个 c
参数之前分配给 c
。
在函数调用之外调用 c = getchar()
解决了这个问题。