条件表达式运算符Conditional-Expression Operator

C 具有一个三元运算符:conditional-expression 运算符 (? :)。C has one ternary operator: the conditional-expression operator (? :).

语法Syntax

conditional-expression:conditional-expression:
logical-OR-expressionlogical-OR-expression

logical-OR expression ?logical-OR expression ? expression : conditional-expressionexpression : conditional-expression

logical-OR-expression 必须具有整型类型、浮点型或指针类型。The logical-OR-expression must have integral, floating, or pointer type. 根据其等效性,其计算结果为 0。It is evaluated in terms of its equivalence to 0. 序列点紧跟 logical-OR-expression。A sequence point follows logical-OR-expression. 操作数的计算将继续,如下所示:Evaluation of the operands proceeds as follows:

  • 如果 logical-OR-expression 不等于 0,则计算 expression。If logical-OR-expression is not equal to 0, expression is evaluated. 由非终止符 expression 给定的表达式的计算结果。The result of evaluating the expression is given by the nonterminal expression. (这意味着,仅当 logical-OR-expression 为 true 时计算 expression。)(This means expression is evaluated only if logical-OR-expression is true.)

  • 如果 logical-OR-expression 等于 0,则计算 conditional-expression。If logical-OR-expression equals 0, conditional-expression is evaluated. 该表达式的结果是 conditional-expression 的值。The result of the expression is the value of conditional-expression. (这意味着,仅当 logical-OR-expression 为 false 时计算 conditional-expression。)(This means conditional-expression is evaluated only if logical-OR-expression is false.)

    请注意,计算 expression 或 conditional-expression,但不同时计算二者。Note that either expression or conditional-expression is evaluated, but not both.

    条件运算的结果类型取决于 expression 或 conditional-expression 操作数的类型,如下所示:The type of the result of a conditional operation depends on the type of the expression or conditional-expression operand, as follows:

  • 如果 expression 或 conditional-expression 具有整型类型或浮点型(其类型可不同),则运算符执行常用算术转换。If expression or conditional-expression has integral or floating type (their types can be different), the operator performs the usual arithmetic conversions. 结果的类型是转换后操作数的类型。The type of the result is the type of the operands after conversion.

  • 如果 expression 和 conditional-expression 都具有相同的结构、联合或指针类型,则结果的类型为相同的结构、联合或指针类型。If both expression and conditional-expression have the same structure, union, or pointer type, the type of the result is the same structure, union, or pointer type.

  • 如果两个操作数都具有类型 void,则结果具有类型 voidIf both operands have type void, the result has type void.

  • 如果其中一个操作数是指向任何类型的对象的指针,且另一个操作数是指向 void 的指针,则指向对象的指针将转换为指向 void 的指针,而结果是指向 void 的指针。If either operand is a pointer to an object of any type, and the other operand is a pointer to void, the pointer to the object is converted to a pointer to void and the result is a pointer to void.

  • 如果 expression 或 conditional-expression 是指针,且另一个操作数是具有值 0 的常量表达式,则结果的类型为指针类型。If either expression or conditional-expression is a pointer and the other operand is a constant expression with the value 0, the type of the result is the pointer type.

    在指针的类型比较中,指针指向的类型中的任何类型限定符(const 或 volatile)是无意义的,但结果类型从两个条件组件中继承限定符。In the type comparison for pointers, any type qualifiers (const or volatile) in the type to which the pointer points are insignificant, but the result type inherits the qualifiers from both components of the conditional.

示例Examples

下面的示例演示条件运算符的用法:The following examples show uses of the conditional operator:

j = ( i < 0 ) ? ( -i ) : ( i );  

此示例将 i 的绝对值赋给 jThis example assigns the absolute value of i to j. 如果 i 小于 0,则将 -i 赋给 jIf i is less than 0, -i is assigned to j. 如果 i 大于或等于 0,则将 i 赋给 jIf i is greater than or equal to 0, i is assigned to j.

void f1( void );  
void f2( void );  
int x;  
int y;  
    .  
    .  
    .  
( x == y ) ? ( f1() ) : ( f2() );  

在此示例中,声明两个函数(f1f2)和两个变量(xy)。In this example, two functions, f1 and f2, and two variables, x and y, are declared. 如果两个变量具有相同的值,则稍后在程序中将调用函数 f1Later in the program, if the two variables have the same value, the function f1 is called. 否则,将调用 f2Otherwise, f2 is called.

另请参阅See Also

条件运算符:? :Conditional Operator: ? :