Home  >  Article  >  Backend Development  >  What is ?: in c language?

What is ?: in c language?

下次还敢
下次还敢Original
2024-04-13 18:36:14897browse

In C language, ?: is a conditional operator, also known as the ternary operator, which can select between two values ​​based on a conditional Boolean expression. The syntax is: condition ? value_if_true : value_if_false. If condition is true, returns value_if_true, otherwise returns value_if_false.

What is ?: in c language?

#What is ?: in C language?

In C language, ?: is a conditional operator, also known as the ternary operator. It allows developers to choose between two different values ​​based on a conditional Boolean expression.

Syntax

?: The syntax of the operator is as follows:

<code class="c">condition ? value_if_true : value_if_false;</code>

where:

  • condition: Conditional Boolean expression. If true, select value_if_true.
  • value_if_true: The first value to select if condition is true.
  • value_if_false: The second value to select if condition is false.

How it works

?: The operator is based on the true or false value of the condition Boolean expression, in value_if_true## Choose between # and value_if_false.

    If
  • condition is true, the operator returns value_if_true.
  • If
  • condition is false, the operator returns value_if_false.

Example

Here is an example of using the ?: operator:

<code class="c">int age = 18;
char *message = (age >= 18) ? "成年人" : "未成年人";</code>
In this example, the ?: operator is based on

age Whether it is greater than or equal to 18, choose between the string "Adult" and "Minor". Since age is greater than or equal to 18, message will be assigned the value "Adult".

The above is the detailed content of What is ?: in c language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn