C语言中的真与假

C语言中在用到循环语句时,我们都会涉及到表达式真假判断,‘真值’有哪些?'假值'有哪些?

我们用代码来实现看看
tf.c                                                                                     
  1 /*********************************************************************************
  2  *      Copyright:  (C) 2018 lingyun
  3  *                  All rights reserved.
  4  *
  5  *       Filename:  tf.c
  6  *    Description:  This file
  7  *                 
  8  *        Version:  1.0.0(07/14/2018)
  9  *         Author:  huangjy <771018493@qq.com>
10  *      ChangeLog:  1, Release initial version on "07/14/2018 02:49:55 PM"
11  *                 
12  ********************************************************************************/
13 #include <stdio.h>
14    
15 int main(void)
16 {  
17     int true_value,false_value;
18    
19     true_value = (2>1);
20     false_value = (2<1);
21    
22     printf("tru = %d, false = %d;\n",true_value,false_value);
23    
24     return 0 ;
25 }  


运行结果:

[a4729821@JYstd c_test]$ gcc tf.c
[a4729821@JYstd c_test]$ ./a.out
tru = 1, false = 0;



trust.c                                                                                  
  2  *      Copyright:  (C) 2018 lingyun
  3  *                  All rights reserved.
  4  *  
  5  *       Filename:  trust.c
  6  *    Description:  This file
  7  *                 
  8  *        Version:  1.0.0(07/14/2018)
  9  *         Author:  huangjy <771018493@qq.com>
10  *      ChangeLog:  1, Release initial version on "07/14/2018 02:34:29 PM"
11  *                 
12  ********************************************************************************/
13 #include <stdio.h>
14     
15 int main(void)
16 {   
17    int n = 4;
18     
19    while(n)
20        printf("%2d is trust;\n",n--);
21     printf("%2d is false;\n",n);
22     
23         n = -4;
24      while(n)
25          printf("%2d is trust;\n",n++);
26      printf("%2d is false;\n",n);
27     
28      return 0;     
29 }   
30     
运行结果:

[a4729821@JYstd c_test]$ gcc trust.c
[a4729821@JYstd c_test]$ ./a.out
4 is trust;
3 is trust;
2 is trust;
1 is trust;
0 is false;
-4 is trust;
-3 is trust;
-2 is trust;
-1 is trust;
0 is false;



总结:表达式为真的值为1,表达式假的值为0;
   while条件判断语句为真就会循环,所以可以知道,一般来说,所有非零值都视为真,只有假被视为假;while(n)“n只要是非零值”都会无限循环;

在C语言里,判断主要围绕逻辑运算符和条件表达式。下面是一些常见的判断口诀: #### 逻辑(&&) “运算两边,整体才为;一边为全为”。逻辑运算符(&&)是双目操作符,使用方式为 `a&&b`,只有当 `&&` 两边的表达式都为时,整个表达式才为,只要有一个为,整个表达式就为 [^1]。 ```c #include <stdio.h> int main() { int a = 1; int b = 2; int c = 0; if (a > 0 && b > 0) { printf("表达式 a > 0 && b > 0 为\n"); } if (a > 0 && c > 0) { printf("表达式 a > 0 && c > 0 为\n"); } else { printf("表达式 a > 0 && c > 0 为\n"); } return 0; } ``` #### 逻辑或(||) “或运算一边,整体即为;两边皆才为”。逻辑或运算符(||)也是双目操作符,使用方式为 `a||b`,只要 `||` 两边的表达式有一个为,整个表达式就为,只有两边都为时,整个表达式才为。 ```c #include <stdio.h> int main() { int a = 1; int b = 2; int c = 0; if (a > 0 || c > 0) { printf("表达式 a > 0 || c > 0 为\n"); } if (c > 0 || (c - 1) > 0) { printf("表达式 c > 0 || (c - 1) > 0 为\n"); } else { printf("表达式 c > 0 || (c - 1) > 0 为\n"); } return 0; } ``` #### 逻辑非(!) “非运算”。逻辑非运算符(!)是单目操作符,用于对一个表达式取反,若表达式为,取反后为;若表达式为,取反后为。 ```c #include <stdio.h> int main() { int a = 1; int c = 0; if (!a) { printf("表达式 !a 为\n"); } else { printf("表达式 !a 为\n"); } if (!c) { printf("表达式 !c 为\n"); } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值