Advertisement
boolean-condition ? first expression : second expression;
/* Conditional Operator Example */
#include<stdio.h>
int main()
{
int a=10, b=20;
int result =a>b ? 10 : 20;
printf("Result1 is %d \n", result);
result = a>b ? 10 : 100.50;
printf("Result2 is %d \n", result);
char ch = a>b ? 'y' : 'n';
printf("Result3 is %c \n", ch);
result = 100>=99 ? 100 : 99;
printf("Result4 is %d \n", result);
return 0;
}
Output
Result1 is 20
Result2 is 100
Result3 is n
Result4 is 100
#include<stdio.h>
int main()
{
int a=10, b=20;
int result = a>b ? 0 : 10;
printf("Result1 is %d \n", result);
char ch = a < b ? : 'n'; /* Intentionally leaving the first expression */
printf("Result2 is %c \n", ch);
return 0;
}
Result1 is 10
Result2 is ☺
Advertisement
#include<stdio.h>
int main()
{
result = 9<2 ? 9 : ;
printf("Result2 is %d \n", result);
}
func41.c:5:20: error: expected expression before ';' token
result = 9<2 ? 9 : ;
^
#include<stdio.h>
int main()
{
char fun(); /* function prototype declaration*/
char result = 100>99? fun() : 'n'; /* Calling function fun() in the first expression */
printf("Result is %c ", result);
return 0;
}
char fun()
{
return 'y';
}
Result is y
Advertisement
Advertisement
Please check our latest addition
PYTHON and DJANGO
Advertisement