Cprogramming 简明教程

Relational Operators in C

C 中的关系运算符被定义为执行两个值的比较。熟悉的尖括号 <> 是关系运算符,此外还有如下表所示的更多关系运算符。

Relational operators in C are defined to perform comparison of two values. The familiar angular brackets < and > are the relational operators in addition to a few more as listed in the table below.

这些关系运算符用于布尔表达式中。所有关系运算符都会计算为 True 或 False。

These relational operators are used in Boolean expressions. All the relational operators evaluate to either True or False.

C 没有布尔数据类型。相反,“0”被解释为 False,任何非零值被视为 True。

C doesn’t have a Boolean data type. Instead, "0" is interpreted as False and any non-zero value is treated as True.

Example 1

以下是 C 中关系运算符的一个简单示例:

Here is a simple example of relational operator in C −

#include <stdio.h>

int main(){

   int op1 = 5;
   int op2 = 3;

   printf("op1: %d op2: %d op1 < op2: %d\n", op1, op2, op1 < op2);

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

op1: 5 op2: 3 op1 < op2: 0

关系运算符在 C 的决策控制和循环语句中起着重要的作用。

Relational operators have an important role to play in decision-control and looping statements in C.

下表列出了 C 中的所有关系运算符:

The following table lists all the relational operators in C −

Operator

Description

Example

==

Checks if the values of two operands are equal or not. If yes, then the condition becomes true.

(A == B)

!=

Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.

(A != B)

>

Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.

(A > B)

<

Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.

(A < B)

>=

Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.

(A >= B)

Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.

(A ⇐ B)

所有关系运算符都是二元运算符。由于它们执行比较,因此需要两侧有两个操作数。

All the relational operators are binary operators. Since they perform comparison, they need two operands on either side.

我们在 C 中使用 = 符号作为赋值运算符。因此,C 使用“ == ”(双等号)作为 equality operator

We use the = symbol in C as the assignment operator. So, C uses the "==" (double equal) as the equality operator.

尖括号 >< 用作“大于”和“小于”运算符。当它们与等号“=”符号组合时,它们形成“大于或等于”的“>=”运算符和“小于或等于”比较的“⇐”运算符。

The angular brackets > and < are used as the "greater than" and "less than" operators. When combined with the "=" symbol, they form the ">=" operator for "greater than or equal" and "⇐" operator for "less than or equal" comparison.

最后,带有“!”前缀的等号“!=”用作 inequality operator

Finally, the "=" symbol prefixed with "!" (!=) is used as the inequality operator.

Example 2

以下示例显示了所有使用中的关系运算符。

The following example shows all the relational operators in use.

#include <stdio.h>

int main(){

   int a = 21;
   int b = 10;
   int c ;

   printf("a: %d b: %d\n", a,b);

   if(a == b){
      printf("Line 1 - a is equal to b\n" );
   } else {
      printf("Line 1 - a is not equal to b\n" );
   }

   if (a < b){
      printf("Line 2 - a is less than b\n" );
   } else {
      printf("Line 2 - a is not less than b\n" );
   }

   if (a > b){
      printf("Line 3 - a is greater than b\n" );
   } else {
      printf("Line 3 - a is not greater than b \n\n" );
   }

   /* Lets change value of a and b */
   a = 5;
   b = 20;

   printf("a: %d b: %d\n", a,b);

   if (a <= b){
      printf("Line 4 - a is either less than or equal to  b\n" );
   }

   if (b >= a){
      printf("Line 5 - b is either greater than  or equal to b\n" );
   }

   if(a != b){
      printf("Line 6 - a is not equal to b\n" );
   } else {
      printf("Line 6 - a is equal to b\n" );
   }

   return 0;
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

a: 21 b: 10
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b

a: 5 b: 20
Line 4 - a is either less than or equal to  b
Line 5 - b is either greater than  or equal to b
Line 6 - a is not equal to b

Example 3

== 运算符需要小心使用。请记住,“ = ”是 C 中的赋值运算符。如果错误地用在等号运算符的位置,则会得到以下不正确的输出:

The == operator needs to be used with care. Remember that "=" is the assignment operator in C. If used by mistake in place of the equality operator, you get an incorrect output as follows −

#include <stdio.h>

int main(){

   int a = 5;
   int b = 3;

   if (a = b){
      printf("a is equal to b");
   }
   else {
      printf("a is not equal to b");
   }

   return 0;
}

Output

“b”的值被分配给非零的“a”,因此 if 表达式返回真。

The value of "b" is assigned to "a" which is non-zero, and hence the if expression returns true.

a is equal to b

Example 4

所有关系运算符的操作数也可以是“char”类型,因为“char”类型是“int”类型的子集。来看一下这个例子:

We can have "char" types too as the operand for all the relational operators, as the "char" type is a subset of "int" type. Take a look at this example −

#include <stdio.h>

int main(){

   char a = 'B';
   char b = 'd';

   printf("a: %c b: %c\n", a,b);

   if(a == b){
      printf("Line 1 - a is equal to b \n");
   } else {
      printf("Line 1 - a is not equal to b \n");
   }

   if (a < b){
      printf("Line 2 - a is less than b \n");
   } else {
      printf("Line 2 - a is not less than b \n");
   }

   if (a > b) {
      printf("Line 3 - a is greater than b \n");
   } else {
      printf("Line 3 - a is not greater than b \n");
   }

   if(a != b) {
      printf("Line 4 - a is not equal to b \n");
   } else {
      printf("Line 4 - a is equal to b \n");
   }

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

a: B b: d
Line 1 - a is not equal to b
Line 2 - a is less than b
Line 3 - a is not greater than b
Line 4 - a is not equal to b

关系运算符不能用于比较次要类型,例如数组或派生类型,例如结构或联合类型。

Relational operators cannot be used for comparing secondary types such as arrays or derived types such as struct or union types.