逻辑运算的结果与逻辑运算中判断变量是否为真的区别
2013-06-14 15:38
351 查看
C语言编译系统在表示 逻辑运算 的 结果 时, 以数值 1 表示 “真” , 以数值 0 表示 “假”。
但在 判断 一个量是否为“真”时,以 0 代表 “假”,以非0代表“真”。
例程:
结果是:
特别是在if条件判断中,不要以为if(-1)不执行!
但在 判断 一个量是否为“真”时,以 0 代表 “假”,以非0代表“真”。
例程:
#include <stdio.h> void Print(int value) { if(value) { printf(" %d is true !!!\n",value); } else { printf(" %d is false !!!\n",value); } } int main(int argc, char **argv) { Print(-1); Print(0); Print(1); printf("================\n"); Print(!(-1)); Print(!(0)); Print(!(1)); return 0; }
结果是:
root@ubuntu:/mnt/hgfs/E/Lessons/MyExercise/DS/3# ./test_real -1 is true !!! 0 is false !!! 1 is true !!! ================ 0 is false !!! 1 is true !!! 0 is false !!! root@ubuntu:/mnt/hgfs/E/Lessons/MyExercise/DS/3#
特别是在if条件判断中,不要以为if(-1)不执行!
相关文章推荐
- 逻辑运算的结果与逻辑运算中判断变量是否为真的区别
- 如何判断除法运算结果是否为整数
- PHP判断变量是否存在及函数isset() 、empty()与is_null的区别
- 逻辑运算符(短路与位)区别 另附逻辑运算符结果列表
- JavaScript判断变量是否为undefined的两种写法区别
- 判断变量是否等于0x20,结果传进来的数值是32
- JavaScript判断变量是否为undefined的两种写法区别
- 用IsNumeric判断表达是的运算结果是否为数字。
- PHP判断变量是否存在及函数isset() 、empty()与is_null的区别
- PHP判断变量是否为空的几种方法区别
- 转:整除运算, 写一个程序,接收两个整数a和b,关判断a是否能被b整除,输出判断结果
- PHP判断变量是否存在及函数isset() 、empty()与is_null的区别
- 整除运算, 写一个程序,接收两个整数a和b,关判断a是否能被b整除,输出判断结果
- shell 判断变量是否为空,变量加不加双引号的区别
- c++中怎么判断一个string类型的字符串变量是否为数字?
- access判断查询的结果是否为空,等同于SQL ISNULL()
- JavaScript判断变量是否为数组的方法(Array)
- 判断C语言变量名是否合法
- 判断对象中的属性是否与同名变量相等
- Java用重载的equals()判断实例变量是否相等