您的位置:首页 > 其它

逻辑运算的结果与逻辑运算中判断变量是否为真的区别

2013-06-14 15:38 281 查看
C语言编译系统在表示 逻辑运算 的 结果 时, 以数值 1 表示 “真” , 以数值 0 表示 “假”。
但在 判断 一个量是否为“真”时,以 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)不执行!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: