Conditional or Ternary operator
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
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )