C 关系和相等运算符C Relational and Equality Operators

二元关系运算符和相等运算符将其第一个操作数与其第二个操作数进行比较以测试指定关系的有效性。The binary relational and equality operators compare their first operand to their second operand to test the validity of the specified relationship. 如果测试的关系为 true,则关系表达式的结果为 1;如果测试的关系为 false,则关系表达式的结果为 0。The result of a relational expression is 1 if the tested relationship is true and 0 if it is false. 结果的类型为 intThe type of the result is int.

语法Syntax

relational-expressionrelational-expression:
shift-expressionshift-expression

relational-expression < shift-expressionrelational-expression < shift-expression

relational-expression > shift-expressionrelational-expression > shift-expression

relational-expression <= shift-expressionrelational-expression <= shift-expression

relational-expression >= shift-expressionrelational-expression >= shift-expression

equality-expression:equality-expression:
relational-expressionrelational-expression

equality-expression == relational-expressionequality-expression == relational-expression

equality-expression != relational-expressionequality-expression != relational-expression

关系运算符和相等运算符测试以下关系:The relational and equality operators test the following relationships:

运算符Operator 测试的关系Relationship Tested
< 第一个操作数小于第二个操作数First operand less than second operand
> 第一个操作数大于第二个操作数First operand greater than second operand
<= 第一个操作数小于或等于第二个操作数First operand less than or equal to second operand
>= 第一个操作数大于或等于第二个操作数First operand greater than or equal to second operand
== 第一个操作数等于第二个操作数First operand equal to second operand
!= 第一个操作数不等于第二个操作数First operand not equal to second operand

上面的列表中的前四个运算符的优先级高于相等运算符(==!=)的优先级。The first four operators in the list above have a higher precedence than the equality operators (== and !=). 请参阅表 C 运算符的优先级和关联性中的优先级信息。See the precedence information in the table Precedence and Associativity of C Operators.

操作数可以具有整型、浮点型或指针类型。The operands can have integral, floating, or pointer type. 操作数的类型可以不同。The types of the operands can be different. 关系运算符对整型和浮点型的操作数执行常用算术转换。Relational operators perform the usual arithmetic conversions on integral and floating type operands. 此外,您可以使用以下操作数类型与关系运算符和相等运算符的组合:In addition, you can use the following combinations of operand types with the relational and equality operators:

  • 任意关系运算符或相等运算符的操作数可以是指向同一类型的指针。Both operands of any relational or equality operator can be pointers to the same type. 对于相等 (==) 运算符和不相等(!=)运算符,比较的结果指示两个指针是否对相同的内存位置寻址。For the equality (==) and inequality (!=) operators, the result of the comparison indicates whether the two pointers address the same memory location. 对于其他关系运算符(<、>、<= 和 >=),比较的结果指示所指向的对象的两个内存地址的相对位置。For the other relational operators (<, >, <=, and >=), the result of the comparison indicates the relative position of the two memory addresses of the objects pointed to. 关系运算符仅比较偏移量。Relational operators compare only offsets.

    仅为同一个对象的部分定义指针比较。Pointer comparison is defined only for parts of the same object. 如果指针引用数组的成员,则比较与相应下标的比较是等效的。If the pointers refer to members of an array, the comparison is equivalent to comparison of the corresponding subscripts. 第一个数组元素的地址“少于”最后一个元素的地址。The address of the first array element is "less than" the address of the last element. 对于结构,指向稍后声明的结构成员的指针“大于”指向之前在结构中声明的成员的指针。In the case of structures, pointers to structure members declared later are "greater than" pointers to members declared earlier in the structure. 指向同一联合的成员的指针是相等的。Pointers to the members of the same union are equal.

  • 指针值可以与常量值 0 进行比较以确定相等 (==) 或不相等 (!=)。A pointer value can be compared to the constant value 0 for equality (==) or inequality (!=). 值为 0 的指针称为“null”指针;即,它不指向有效的内存位置。A pointer with a value of 0 is called a "null" pointer; that is, it does not point to a valid memory location.

  • 相等运算符遵循与关系运算符相同的规则,但允许其他可能性:可以将指针与值为 0 的常量整数表达式进行比较或将指针与指向 void 的指针进行比较。The equality operators follow the same rules as the relational operators, but permit additional possibilities: a pointer can be compared to a constant integral expression with value 0, or to a pointer to void. 如果两个指针都是 null 指针,则它们的比较结果相等。If two pointers are both null pointers, they compare as equal. 相等运算符比较段和偏移量。Equality operators compare both segment and offset.

示例Examples

下面的示例阐释了关系运算符和相等运算符。The examples below illustrate relational and equality operators.

int x = 0, y = 0;  
if ( x < y )  

由于 xy 相等,因此该示例中的表达式会生成值 0。Because x and y are equal, the expression in this example yields the value 0.

char array[10];  
char *p;  

for ( p = array; p < &array[10]; p++ )  
    *p = '\0';  

此示例中的片段将 array 的每个元素设置为 null 字符常量。The fragment in this example sets each element of array to a null character constant.

enum color { red, white, green } col;  
   .  
   .  
   .  
   if ( col == red )  
   .  
   .  
   .  

这些语句声明带标记 col 的名为 color 的枚举变量。These statements declare an enumeration variable named col with the tag color. 在任何时候,变量可以包含整数值 0、1 或 2,这表示枚举集 color 的某个元素:分别为红色、白色或绿色。At any time, the variable may contain an integer value of 0, 1, or 2, which represents one of the elements of the enumeration set color: the color red, white, or green, respectively. 如果在执行 if 语句时,col 包含 0,则将执行任何依赖 if 的语句。If col contains 0 when the if statement is executed, any statements depending on the if will be executed.

另请参阅See Also

关系运算符:<、>、<= 和 >= Relational Operators: <, >, <=, and >=
相等运算符:== 和 !=Equality Operators: == and !=