next up previous contents
Next: Bitwise 運算子 Up: 運算子 Previous: 關係運算子   Contents

邏輯運算子

  1. 邏輯運算子 (Logical Operators) 說明
    符號 意義 範例 說明
    && AND a&&b a AND b
    || OR a||b a OR b
    ! NOT !a NOT a
  2. 實例說明:
    [dywang@dyw219 zzz]$ vim op-log1.c 
    [dywang@dyw219 zzz]$ cat op-log1.c 
    #include <stdio.h>
    
    main() {
        int a, b;
    
        printf("Enter TWO integers: ");
        scanf("%d %d", &a, &b);
    
        if ( a>0 && b>0 ) printf("%d>0 AND %d>0\n", a, b );
        if ( a>0 || b>0 ) printf("%d>0 OR %d>0\n", a, b );
        if ( !( a>0 ) ) printf("NOT %d>0\n", a );
    }
    
  3. 編譯成功
    [dywang@dyw219 zzz]$ gcc -o op-log1 op-log1.c
    
  4. 執行結果一
    [dywang@dyw219 zzz]$ ./op-log1 
    Enter TWO integers: 2 3
    2>0 AND 3>0
    2>0 OR 3>0
    
  5. 執行結果二
    [dywang@dyw219 zzz]$ ./op-log1 
    Enter TWO integers: 2 -3
    2>0 OR -3>0
    
  6. 執行結果三
    [dywang@dyw219 zzz]$ ./op-log1 
    Enter TWO integers: -2 5
    -2>0 OR 5>0
    NOT -2>0
    



De-Yu Wang 2019-09-19