Home / Questions / What is the use of a ? character?
Explanatory Question

What is the use of a ? character?

👁 930 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

In the C programming language, the question mark (?) is used as part of the ternary conditional operator, also known as the ternary operator or the conditional operator. The syntax of the ternary operator is:


condition ? expression_if_true : expression_if_false;

Here's how it works:

  • The condition is evaluated.
  • If the condition is true, the entire expression evaluates to expression_if_true.
  • If the condition is false, the entire expression evaluates to expression_if_false.

Here's a simple example:


int x = 10;
int y = 20;

int max = (x > y) ? x : y;

In this example, if x is greater than y, then the value of max will be x; otherwise, it will be y. The ternary operator provides a concise way to express a conditional assignment.

It's important to note that while the ternary operator can make code more compact, using it excessively or in complex expressions can reduce code readability. It's recommended to use it judiciously for simple conditional assignments to maintain code clarity.