The ? in C is used as a conditional operator to return different values based on conditions. Syntax: condition ? true value : false value. Can be used to quickly change variable values or select operations. Conditional operators can be nested, and different conditions return different truth values.
#What does ? in C mean?
In C, the ? symbol is used for conditional operators (also called ternary operators). A conditional operator is a shorthand form for evaluating a condition and returning a different value depending on whether the condition is true or false.
Syntax:
<code>条件 ? 真值 : 假值</code>
Where:
Usage:
Conditional operators are often used to quickly change the value of a variable or select a different action based on a condition. For example:
<code class="c++">int age = 25; std::string message = (age >= 18) ? "成年人" : "未成年人";</code>
In this example, message
is assigned different values based on the value of age
. If age
is greater than or equal to 18, message
is "adult"
, otherwise "minor"
.
Nested conditional operators:
Conditional operators can be nested, which means that the true or false value of one conditional operator can be the true or false value of another conditional operator. symbol. For example:
<code class="c++">int score = 90; char grade = (score >= 90) ? 'A' : (score >= 80) ? 'B' : 'C';</code>
In this example, grade
is assigned a different value based on the value of score
:
If score
is greater than or equal to 90, then grade
is 'A'. score
is greater than or equal to 80, but less than 90, then grade
is 'B'. grade
is 'C'. The above is the detailed content of What does ? mean in c++?. For more information, please follow other related articles on the PHP Chinese website!