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

相关文章:

  • 2021-08-25
  • 2021-11-04
  • 2021-06-15
  • 2021-10-19
  • 2021-11-07
  • 2022-12-23
猜你喜欢
  • 2021-11-14
  • 2021-05-24
  • 2021-05-21
  • 2021-08-03
  • 2021-10-16
  • 2021-09-02
  • 2021-10-03
相关资源
相似解决方案