NO.288 条件表达式等价
Q:若有条件表达式(M)?a++:a--,其中表达式M?A.和(M==0)等价
B.和(M==1)等价
C.和(M!=0)等价
D.和(M!=1)等价
为什么只有C可以等价,B为什么不行?
#include <stdio.h>
#include <math.h>
int main()
{
int M,a;
printf("%d\n",((M)?a++:a--));
return 0;
}
为什么运行结果是1?
因为 M 也可以是 2, 3, 4, ....... (只要不是 0,负数也可以) #include <stdio.h>
int main(){
int a = 0;
int b = 1;
int c = 123;
int d = -456;
if(a) printf("a is true\n");
else printf("a is false\n");
if(b) printf("b is true\n");
else printf("b is false\n");
if(c) printf("c is true\n");
else printf("c is false\n");
if(d) printf("d is true\n");
else printf("d is false\n");
}a is false
b is true
c is true
d is true
页:
[1]