Ternary Operator - C Operator

C examples for Operator:Conditional Operator

Introduction

The ternary operator ?: can replace a single if/else statement.

This operator takes three expressions.

If the first one is true then the second expression is evaluated and returned; and if it is false, the third one is evaluated and returned.

Demo Code

#include <stdio.h>
int main(void) {
    int x = 0;/*w w w .  java  2 s .c  o  m*/
    x = (x < 0.5) ? 0 : 1; /* ternary operator (?:) */
    printf("%d",x);
}

Result


Related Tutorials