Cprogramming 简明教程

Ternary Operator in C

C 中的三元运算符 (?:) 是一种条件运算符。术语 “三元” 暗示运算符有三个操作数。三元运算符通常用于以更简洁的方式放置多个条件(if-else)语句。

The ternary operator (?:) in C is a type of conditional operator. The term "ternary" implies that the operator has three operands. The ternary operator is often used to put multiple conditional (if-else) statements in a more compact manner.

Syntax of Ternary Operator in C

三元运算符与以下语法一起使用:

The ternary operator is used with the following syntax −

exp1 ? exp2 : exp3

它使用三个 operands

It uses three operands

  1. exp1 − A Boolean expression evaluating to true or false

  2. exp2 − Returned by the ? operator when exp1 is true

  3. exp3 − Returned by the ? operator when exp1 is false

Example 1: Ternary Operator in C

下面的 C 程序使用三元运算符检查变量是否为偶数或奇数。

The following C program uses the ternary operator to check if the value of a variable is even or odd.

#include <stdio.h>

int main(){

   int a = 10;

   (a % 2 == 0) ? printf("%d is Even \n", a) : printf("%d is Odd \n", a);

   return 0;
}

Output

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

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

10 is Even

将 "a" 的值更改为 15 并再次运行代码。现在,你将获得以下 output

Change the value of "a" to 15 and run the code again. Now you will get the following output

15 is Odd

Example 2

条件运算符是 if-else 结构的紧凑表示。我们可以通过以下代码来改写检查奇数/偶数的逻辑 −

The conditional operator is a compact representation of if–else construct. We can rewrite the logic of checking the odd/even number by the following code −

#include <stdio.h>

int main(){

   int a = 10;

   if (a % 2 == 0){
      printf("%d is Even\n", a);
   }
   else{
      printf("%d is Odd\n", a);
   }

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

10 is Even

Example 3

下面的程序比较变量 "a" 和 "b",并将其中一个较大值分配给变量 "c"。

The following program compares the two variables "a" and "b", and assigns the one with the greater value to the variable "c".

#include <stdio.h>

int main(){

   int a = 100, b = 20, c;

   c = (a >= b) ? a : b;

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

   return 0;
}

Output

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

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

a: 100 b: 20 c: 100

Example 4

使用 if-else 结构所对应的代码如下 −

The corresponding code with if–else construct is as follows −

#include <stdio.h>

int main(){

   int a = 100, b = 20, c;

   if (a >= b){
      c = a;
   }
   else {
      c = b;
   }
   printf ("a: %d b: %d c: %d\n", a, b, c);

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

a: 100 b: 20 c: 100

Example 5

如果你需要在三元运算符的 true 和/或 false 操作中输入多个语句,那么你必须用逗号将它们分开,如下所示 −

If you need to put multiple statements in the true and/or false operand of the ternary operator, you must separate them by commas, as shown below −

#include <stdio.h>

int main(){

   int a = 100, b = 20, c;

   c = (a >= b) ? printf ("a is larger "), c = a : printf("b is larger "), c = b;

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

   return 0;
}

Output

在此代码中,较大数分配给 "c",同时还会打印相应的消息。

In this code, the greater number is assigned to "c", along with printing the appropriate message.

a is larger a: 100 b: 20 c: 20

Example 6

使用 if-else 语句的相应程序如下 −

The corresponding program with the use of if–else statements is as follows −

#include <stdio.h>

int main(){

   int a = 100, b = 20, c;

   if(a >= b){
      printf("a is larger \n");
      c = a;
   }
   else{
      printf("b is larger \n");
      c = b;
   }
   printf ("a: %d b: %d c: %d\n", a, b, c);

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

a is larger
a: 100 b: 20 c: 100

Nested Ternary Operator

就像我们可以使用嵌套的 if-else 语句一样,我们可以在 True 操作数和 False 操作数中使用三元运算符。

Just as we can use nested if-else statements, we can use the ternary operator inside the True operand as well as the False operand.

exp1 ? (exp2 ? expr3 : expr4) : (exp5 ? expr6: expr7)

首先,C 检查 expr1 是否为 true。如果是,则检查 expr2。如果为 true,则结果为 expr3;如果为 false,则结果为 expr4。

First C checks if expr1 is true. If so, it checks expr2. If it is true, the result is expr3; if false, the result is expr4.

如果 expr1 变为 false,则它可能会检查 expr5 是否为 true,然后返回 expr6 或 expr7。

If expr1 turns false, it may check if expr5 is true and return expr6 or expr7.

Example 1

让我们开发一个 C 程序来确定一个数字是能被 2 和 3 整除,还是仅能被 2 整除,或仅能被 3 整除,还是不能被 2 和 3 整除。我们将为此使用嵌套条件运算符,如下面的代码所示 −

Let us develop a C program to determine whether a number is divisible by 2 and 3, or by 2 but not 3, or 3 but not 2, or neither by 2 and 3. We will use nested condition operators for this purpose, as shown in the following code −

#include <stdio.h>

int main(){

   int a = 15;
   printf("a: %d\n", a);

   (a % 2 == 0) ? (
      (a%3 == 0)? printf("divisible by 2 and 3") : printf("divisible by 2 but not 3"))
   : (
      (a%3 == 0)? printf("divisible by 3 but not 2") : printf("not divisible by 2, not divisible by 3")
   );

   return 0;
}

检查不同的值 −

Check for different values −

a: 15
divisible by 3 but not 2

a: 16
divisible by 2 but not 3

a: 17
not divisible by 2, not divisible by 3

a: 18
divisible by 2 and 3

Example 2

在此程序中,我们针对相同目的使用了嵌套 if-else 语句代替条件运算符 −

In this program, we have used nested if–else statements for the same purpose instead of conditional operators −

#include <stdio.h>

int main(){

   int a = 15;
   printf("a: %d\n", a);

   if(a % 2 == 0){
      if (a % 3 == 0){
         printf("divisible by 2 and 3");
      }
      else {
         printf("divisible by 2 but not 3");
      }
   }
   else{
      if(a % 3 == 0){
         printf("divisible by 3 but not 2");
      }
      else {
         printf("not divisible by 2, not divisible by 3");
      }
   }

   return 0;
}

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

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

a: 15
divisible by 3 but not 2