C – Conditional Operators

      Prev                                                                                              Next

Conditional or ternary operators in C:

    • Conditional operators return one value if condition is true and returns another value is condition is false.
    • This operator is also called as ternary operator.
Syntax     :        (Condition? true_value: false_value);
Example :        (A > 100  ?  0  :  1);
.
    • In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

Example program for conditional/ternary operators in C:

#include <stdio.h>
int main()
{
        int x=1, y ;
        y = ( x ==1 ? 2 : 0 ) ;
        printf("x value is %dn", x);
        printf("y value is %d", y);
}

Output:

x value is 1
y value is 2

……

 Continue on types of C operators:

    • Click on each operators name below to display example programs.
S.no
Types of Operators
             Description              
1
Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
2
Assignment operators These are used to assign the values for the variables in C programs.
3
Relational operators These operators are used to compare the value of two variables.
4
Logical operators These operators are used to perform logical operations on the given two variables.
5
Bit wise operators These operators are used to perform bit operations on given two variables.
6
Conditional(ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false.
7
Increment / decrement operators These operators are used to either increase or decrease the value of the variable by one.
8
Special operators &, *, sizeof( ) and ternary operators

      Prev                                                                                              Next

.