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

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

< 第一个操作数小于第二个操作数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

• 任意关系运算符或相等运算符的操作数可以是指向同一类型的指针。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

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

``````char array[10];
char *p;

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

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