c programming trinary if

#include <stdio.h>

int main() {
    int num = 10;

    num > 10 ? printf("Greater than 10\n") : (num < 10 ? printf("Less than 10\n") : printf("Equal to 10\n"));

    return 0;
}

Explanation:

  • #include <stdio.h>: Includes the standard input-output library in the program.

  • int main() { }: Defines the main function where the execution of the program begins and ends.

  • int num = 10;: Declares an integer variable num and initializes it with the value 10.

  • num > 10 ? printf("Greater than 10\n") : (num < 10 ? printf("Less than 10\n") : printf("Equal to 10\n"));:

  • This line uses the ternary conditional operator (?:) to check if num is greater than, less than, or equal to 10.
  • If num is greater than 10, it prints "Greater than 10\n".
  • If num is less than 10, it prints "Less than 10\n".
  • If num is neither greater nor less than 10 (which means it's equal to 10), it prints "Equal to 10\n".

  • return 0;: Indicates the successful execution of the main() function and the program termination.