C Language Learning Notes: The Ternary Operator – The Only Ternary Operator

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basics of C language.

“C Language Beginner’s Essential Knowledge Notes Series – 100 Articles”

22. The Ternary Operator: The Only Ternary Operator – Using These Techniques Makes Conditional Judgments Very Concise

1. Basic Concept of the Ternary Operator

The ternary operator (?:) is the only ternary operator in C language, which has three operands and provides a concise syntax for conditional expressions:

conditional_expression ? expression1 : expression2

Execution flow:

  1. 1. First, evaluate the value of <span>conditional_expression</span>.
  2. 2. If <span>conditional_expression</span> is true (non-zero), evaluate and return the value of <span>expression1</span>.
  3. 3. If <span>conditional_expression</span> is false (zero), evaluate and return the value of <span>expression2</span>.

2. Basic Usage Examples

1. Simple Conditional Judgment

int a = 5, b = 10;
int max = (a > b) ? a : b;  // max = 10

2. Output Result Formatting

int score = 65;
printf("Score: %s\n", (score >= 60) ? "Pass" : "Fail");

3. Getting the Absolute Value

int x = -5;
int abs_x = (x >= 0) ? x : -x;

3. Operator Precedence and Associativity

1. Precedence and Associativity

  • • Precedence: lower than arithmetic and relational operators, higher than assignment operators
  • • Associativity: right to left
int a = 1, b = 2, c = 3;
int result = a ? b : c ? 4 : 5;  // Equivalent to a ? b : (c ? 4 : 5)

2. Type Conversion Rules

  • • If <span>expression1</span> and <span>expression2</span> are of different types, automatic type conversion occurs
  • • The conversion rules are the same as for ordinary arithmetic operations: convert towards the type with a larger size
int x = 5;
double y = 3.14;
double result = (x > 0) ? x : y;  // int automatically converted to double

4. Comparison with if-else

Feature Ternary Operator if-else Statement
Syntax Form Expression Statement
Return Value Always returns a value No return value (can perform actions)
Code Conciseness Concise, suitable for simple conditions Clear, suitable for complex logic
Readability Reduces readability when nested too much Structure is clear
Execution Efficiency Usually the same (after compiler optimization) Usually the same

5. Common Mistakes

1. Missing Parentheses Leading to Precedence Issues

int x = 5, y = 10;
int max = x > y ? x : y + 1;  // Actually x > y ? x : (y + 1)

Correction:

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

6. Special Application Scenarios

1. Initializing const Read-Only Variables or Constants

// const read-only variable, initialized once at runtime
const char *status = (isReady) ? "Ready" : "Not Ready";

2. Function Parameter Passing

printf("Value: %d\n", (debugMode) ? getDebugValue() : getNormalValue());

3. Conditional Selection in Macro Definitions

#define MAX(a,b) ((a) > (b) ? (a) : (b))

Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but I think having a group would indeed be convenient, so I will try to create one this time.

If you need it, hurry up and join; the validity period is 7 days.

C Language Learning Notes: The Ternary Operator - The Only Ternary Operator

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, please feel free to use them; the views are for learning reference only~~]

C Language Learning Notes: The Ternary Operator - The Only Ternary Operator

C Language Learning Notes: The Ternary Operator - The Only Ternary Operator

“If you like C, please like it”C Language Learning Notes: The Ternary Operator - The Only Ternary Operator Click the bottom right corner to seeC Language Learning Notes: The Ternary Operator - The Only Ternary Operator

Leave a Comment