Skip to main content

Conditional (Ternary) Operator in C

1 min read Updated June 30, 2026
Share:
On this page (9sections)

Conditional or Ternary operator

Definition

Check condtion if true,it returns first varibales value otherwise return second values. sometimes it replaces if..else statement

Syntax

Condition? Expression1: Expression2

Example

(a>10) ? b : c

Explanation For Conditional or Ternary operator

Given that

a, b, c

are expressions;

the expression

(a>10) ? b : c

has as its value b if a is nonzero, and c otherwise. Only expression b or c is evaluated.

Expressions b and c must be of the same data type. If they are not, but are both arithmetic data types, the usual arithmetic conversions are applied to make their types the same. It is also called ternary operators.

Example Program For Conditional or Ternary operator

#include <stdio.h>

//Conditional or Ternary operator Example Program In C

void main() {

  int a = 10;

  int b = 15;

  int c;

  c = a <= b ? a : b;

  printf("C Is %d", c);

}

Sample Output:

C Is 10

Continue learning with these related tutorials and programs:

Frequently Asked Questions

What does this C program do?
It is a C example program that demonstrates Conditional (Ternary) Operator, including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses core C syntax, illustrating a common pattern in C programming.

Related Tutorials

Search tutorials