Tutorials

C Operator


C Ternary operator


In C programming, ternary operator takes three argumants. A ternary operator may also be represented as ?:

Lets take this statement for example,

Expression ? statement1 : statement2

How the above expression with ternary operator evaluates?

  • First the 'Expression' is checked and it returns a result which is either true or false.
  • If result of the 'Expression' is true then statement1 is executed
  • Else statement2 is executed.

 

Example of ternary operator

#include <stdio.h>
#include<conio.h>

void main()
{
   int a ;
   short b;
   double c;

    printf("    OUTPUT    \n");

   /* example of ternary operator */
   a = 10;
   b = (a == 1) ? 20: 30;
   printf( "Value of b is %d\n", b );

   b = (a == 10) ? 20: 30;
   printf( "Value of b is %d\n", b );

}

OUTPUT

The output of the above code is shown in below screenshot.

C ternary operator

 

 

 



Please Share this page
Views : 14
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!