Skip to main content

Conditional operator

The conditional operator (?:) is an operator that evalues an expression and results in one of two values depending on whether the condition was truthy or not

condition?v1:v2

if condition is truthy, expression evaluates to v1, otherwise it evalues to v2

This program asks user to enter a number and prints out whether it is positive or not positive

#include <stdio.h>
int main(void){
int number;
printf("enter a number: ");
scanf("%d", &number);
number > 0?printf("%d is positive",number):printf("%d is not positive",number);

}