【发布时间】:2021-02-09 13:27:42
【问题描述】:
我想打印基于mode 值的变量数。这是一个例子:
char format[64] = {}
int mode_1, mode_2, mode_3;
int a,b,c,d,e,f;
...
// Get mode value here ....
...
// Build the format first
sprintf(format, "%s-%s-%s\n", mode_1 == 0 ? "a=%d,b=%d" : "a=%d",
mode_2 == 0 ? "c=%d,d=%d" : "c=%d",
mode_3 == 0 ? "e=%d,f=%d" : "e=%d");
// Print value here
printf(format, mode_1 == 0 ? (a,b) : a,
mode_2 == 0 ? (c,d) : c,
mode_3 == 0 ? (e,f) : f);
当我尝试一个简单的例子时,当模式值为零时打印的值似乎不正确。我在这里做错了什么?
【问题讨论】:
-
When I tried a simple example请不要发布截图。请将文本作为文本发布。 -
?:是一个运算符,因此它有 1 个结果值。在这两种情况下,您不能使用它来评估多个值甚至不同数量的结果。
标签: c format printf conditional-operator comma-operator