ตัวดำเนินการเชิงสัมพันธ์ (Relational Operators)
ใช้เปรียบเทียบค่าสองค่า คืนค่า 1 หากจริง, 0 หากเท็จ
| ตัวดำเนินการ | ชื่อ |
|---|---|
| == | Equal to |
| > | Greater than |
| < | Less than |
| != | Not equal to |
| >= | Greater than or equal to |
| <= | Less than or equal to |
ตัวอย่าง
#include <stdio.h>
int main() {
int x = 10, y = 28;
if (x == y) printf("Both variables are equal\n");
if (x > y) printf("x is greater than y\n");
if (x < y) printf("x is less than y\n");
if (x != y) printf("x is not equal to y\n");
if (x <= y) printf("x is lesser or equal to y\n");
if (x >= y) printf("x is greater or equal to y\n");
return 0;
}ผลลัพธ์
x is less than y x is not equal to y x is lesser or equal to y
ตัวดำเนินการตรรกะ (Logical Operators)
ใช้ทำงานเชิงตรรกะ คืนค่า 0 หรือ 1 ใช้ในการตัดสินใจ
| ตัวดำเนินการ | ความหมาย | ผลลัพธ์ |
|---|---|---|
| && | Logical AND | จริงเมื่อทุก operand จริง |
| || | Logical OR | จริงถ้ามี operand อย่างน้อยหนึ่งจริง |
| ! | Logical NOT | จริงเมื่อ operand เป็นศูนย์ |
ตัวอย่าง
#include <stdio.h>
int main() {
int x = 10, y = 28, a = 15, b = 20;
if (x < y && a == b) printf("x is less than y AND a is equal to b\n");
if (x < y || a == b) printf("x is less than y OR a is equal to b\n");
if (!x) printf("x is zero\n");
return 0;
}ผลลัพธ์
x is less than y OR a is equal to b