Nearby lessons

31 of 72

C - Conditional (Ternary) Operator

Conditional (Ternary) Operator is one of the foundational topics in C programming. This lesson explains Conditional (Ternary) Operator (?:) and Program: check pass or fail with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Conditional (Ternary) Operator (?:)

This is a one-line shortcut for a simple if-else. It picks one of two values based on a condition.

In simple words: the ternary operator asks one yes/no question and hands you one of two answers. Read ? as "then" and : as "otherwise": marks >= 40 ? 'P' : 'F' → "if marks ≥ 40 then 'P', otherwise 'F'".
Example01
Loading editor…

Program: check pass or fail

Example02
Loading editor…
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.