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

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)不执行!

作者: TigerXiao

Email: tiger.xiaowh001@gmail.com

出处: 老虎工作室

说明: 欢迎讨论、转载,转载请注明出处。

原文地址:https://www.cnblogs.com/xiaowenhu/p/3135699.html