Conditional operators in c

conditional operator is also called as ternary operator .it is similar to if-else statements .it has total three statements .the first statements is condition expression.

syntax:

conditionalExpression ? expression1 : expression2

The conditional operator works as follows:

  1. The first expression conditional Expression is evaluated at first. This expression evaluates to 1 if it’s and evaluates to 0 if it’s false.

  2. If conditional Expression is true, expression1 is evaluated.

  3. If conditional Expression is false, expression2 is evaluated.

#include
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to 29.
// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
//codeskulls

output:

If this year is leap year, enter 1. If not enter any integer: 1
Number of days in February = 29
Repl.it: Run your Repl to interact with it