How to use ternary operator in c language

Ternary Operator

On this page

Ternary Operator

Program to use Ternary Operator in C Language

#include<stdio.h>
  int main()
  {
    int y,z,max;
    printf("Enter values for y and z : ");
    scanf("%d%d",&y, &z);
    max = z>y ? y : z;
    printf("larger of %d and %d is %d\n",y, z, max);
    return 0;
  }
  

Output

Enter a values for y and z : 12
  4
  larger of 12 and 4 is 12
  

Steps Description

  • Declare integer three variable y , z and max
  • printf inbuild function to display value of variable y and z with respective decimal placeholder %d
  • Evaluation of variable y and z to determine greater than z
  • Assign max variable with greater than variable y
  • printf inbuild function to display larger value of variable y