C 庫 - <iso646_h.h>



C 庫標頭檔案 <iso646_h.htm> 允許使用替代運算子,例如 andxornot 等,它們返回特定值。例如,在布林表示式中使用 "and" 代替 && 可以使程式碼更易讀。

有十一個宏派生自 iso646.h 標頭檔案:

標記
and &&
and_eq &=
bitand &
bitor |
compl ˜
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

示例

以下是 C 庫標頭檔案 <iso646_h.htm> 的演示,使用替代運算子 ('and') 對兩個數字進行運算。

#include <stdio.h>
#include <iso646.h>

int main() {
   int a = 5;
   int b = 3;
   
   // Using the alternative 'and' operator
   int sum = a and b; 

   printf("Sum of %d and %d = %d\n", a, b, sum);
   return 0;
}

輸出

以上程式碼產生以下結果:

Sum of 5 and 3 = 1

示例

我們建立了一個使用替代運算子 (xor) 交換兩個數字的程式。

#include <stdio.h>
#include <iso646.h>

int main() {
   int x = 5;
   int y = 3;

   // Using the alternative 'xor' operator
   x = x xor y; 
   y = x xor y;
   x = x xor y;

   printf("After swapping: x = %d, y = %d\n", x, y);
   return 0;
}

輸出

執行上述程式碼後,我們得到以下結果:

After swapping: x = 3, y = 5

示例

下面的程式使用替代運算子計算兩個值的邏輯“and”。

#include <stdio.h>
#include <iso646.h>
int main() {
    int bool1 = 1;
    int bool2 = 0;

    int result = bool1 and bool2; 

    printf("Logical AND of %d and %d = %d\n", bool1, bool2, result);
    return 0;
}

輸出

執行程式碼後,我們得到以下結果:

Logical AND of 1 and 0 = 0
廣告