简体   繁体   English

C中三元运算符中的条件表达式

[英]Conditional expression in ternary operator in C

Code代码

#include <stdio.h>

int main() {
  int i;
  for (i=1; i<=10; i++) {
        (i % 2) ? printf("%d is odd\n", i) : printf("%d is even\n", i);
  }
}

Result结果

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even

In the above C program, why it still works fine even though the conditional expression only states i%2 and not i%2!=0 ?在上面的 C 程序中,为什么即使条件表达式只说明i%2而不是i%2!=0 ,它仍然可以正常工作?

In C, integers can be used in a Boolean context, and zero represents false while non-zero represents true.在 C 中,整数可用于布尔上下文,零表示假,非零表示真。

That's why your code works.这就是您的代码有效的原因。 The expression num % 2 will be 0 (the single false value) for an even number and 1 (one of the many possible true values) for an odd number.表达式num % 2对于偶数将为 0(单个假值),对于奇数为 1(许多可能的真值之一)。

The following expressions would all work for detecting an odd number:以下表达式都适用于检测奇数:

num % 2
(num % 2) != 0
((num % 2) != 0) != 0
... and so on, ad untilyougetboredum (like 'ad infinitum' but with limits).

Having said that, I don't really consider it a good idea to do it this way, code should express intent as much as possible and the intent here should be to choose the path of execution based on a comparison.话虽如此,我并不认为这样做是一个好主意,代码应该尽可能地表达意图,这里的意图应该是根据比较选择执行路径。 That means, if you're looking for an odd number, you should use something like (num % 2) == 1 .这意味着,如果你正在寻找一个奇数,你应该使用类似(num % 2) == 1

You also don't need a separate printf call in each of those code paths:您也不需要在每个代码路径中进行单独的printf调用:

printf("%d is %s\n", num, ((num % 2) == 1) ? "odd" : "even");

You'll notice I've also used num instead of i .您会注意到我还使用了num而不是i This is simply a style thing of mine, related to the afore-mentioned intent.这只是我的风格,与上述意图有关。 If the variable is only used as an index, I'm happy to use the i -type variables (a) but, the second it gains a semantic property (like a number being checked for oddity), I tend to use more descriptive names.如果变量仅用作索引,我很乐意使用i类型变量(a)但是,第二个它获得语义属性(例如检查数字是否奇怪),我倾向于使用更具描述性的名称.

I have no issue with people using simple variable names, I just prefer more descriptive ones in my own code.我有一个使用简单的变量名的人没有问题,我只是喜欢更多的描述那些在我自己的代码。


(a) Actually, I'd probably use idx in that case but that's being too CDO (b) , even for me :-) (a)实际上,在那种情况下我可能会使用idx但这CDO (b) ,即使对我来说:-)


(b) OCD but in the right order :-) (b)强迫症,但顺序正确:-)

C doesn't have a dedicated boolean type. C 没有专用的布尔类型。 It uses int value as boolean.它使用 int 值作为布尔值。 That is 0 is considered false and any non zero value is treated as true .即 0 被认为是false ,任何非零值都被视为true Try printing some conditions尝试打印一些条件

printf("%d",5==5);
printf("%d",1>3);

This will output 1 and 0.这将输出 1 和 0。

C always uses 1 to denote true . C 总是使用 1 来表示true But any other non-zero value would work as well when using in conditions.但是在条件中使用时,任何其他非零值也可以正常工作。

if(6+1)
    printf("TRUE");

Will print TRUE.将打印 TRUE。

This is also the reason we can use this form of while loop:这也是我们可以使用这种形式的 while 循环的原因:

int i= 10;
while(i--){
    printf("%d",i);
}

Will print 9876543210 .将打印9876543210 Notice it stops when i becomes 0, which is false .请注意,当 i 变为 0 时它停止,这是false

Now back to the question, i%2 would always result in either 0 or 1. In case of 1(true) the first statement is run while in case of 0 (false) the second statement is run.现在回到问题, i%2总是会导致 0 或 1。在 1(真)的情况下运行第一个语句,而在 0(假)的情况下运行第二个语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM