Conditional Operator in C ( ?: ) with Example

Conditional Operator in C ( ?: ) with Example

A conditional Operator in C is also called a Ternary operator; In this tutorial, you will learn everything about conditional or ternary operators in c programming with examples.

This tutorial will take several examples as follows:

  • C program to find maximum between two numbers using conditional operator
  • To check whether the given number is positive or negative using conditional operator in c
  • Find the given number is odd or even using the conditional operator in c
  • Find the largest among three numbers using the conditional operator in C

Conditional or Ternary Operator in C ( ?: ) with Example

C programming conditional operator is also known as a ternary operator. It takes three operands. Conditional operator is closely related with if..else statement.

Note that:- Conditional or ternary operators is used in the decision-making process.

Syntax of Conditional Operator in C

expression1 ? expression2 : expression3;

or for simplicity, we write it as

condition ? true-statement : false-statement;

Example 1 – C program to find maximum between two numbers using conditional operator

Using the conditional and ternary operator in c programming; you can find maximum between two numbers using conditional operator; as shown below:

#include<stdio.h>
int main()
{
  float num1, num2, max;

  printf("Enter two numbers: ");
  scanf("%f %f", &num1, &num2);

  max = (num1 > num2) ? num1 : num2;

  printf("Maximum of %.2f and %.2f = %.2f",
                num1, num2, max);

  return 0;
}

Output:-

Enter two numbers: 12.5 10.5
Maximum of 12.50 and 10.50 = 12.50

Explanation of the above c program:

  • First take input two number from user in program
  • Then expression, (num1 > num2) is evaluated.
  • num1=12.5 and num2=10.5; so expression (num1>num2) becomes true.
  • Hence num1 is the maximum number, and it is assigned to the variable max.

Example 2 – To check whether the given number is positive or negative using conditional operator in c

Using the conditional and ternary operator in c programming; you can check whether the given number is positive or negative using conditional operator in c ; as shown below:

#include<stdio.h>
int main()
{
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  (num>=0)?printf("Positive."):printf("Negative");
  return 0;
}

Output:-

Enter a number: 8
Positive.

Explanation of the above c program:

  • First take input number from user in program.
  • Use ternary or conditional operator to find positive or negetive number.
  • Print result.

Example 3 – Find the given number is odd or even using the conditional operator in c

Using the conditional and ternary operator in c programming; You can find the given number is odd or even using the conditional operator ; as shown below:

#include<stdio.h>
int main()
{
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  (num%2==0)? printf("Even"): printf("Odd");
  return 0;
}

Output:-

Enter a number: 9
Odd

Explanation of the above c program:

  • First take input number from user in program.
  • Use ternary or conditional operator to find even or odd number from user input number.
  • Print result.

Example 4 – Find the largest among three numbers using the conditional operator in C

Using the conditional and ternary operator in c programming; You can find the largest among three numbers using the conditional operator in c.; as shown below:

#include<stdio.h>
int main()
{
  float a, b, c, max;
  printf("Enter three numbers: ");
  scanf("%f %f %f",&a, &b, &c);
  max = (a>b && a>b)? a: (b>a && b>c)? b:c;
  printf("Maximum number = %.2f",max);
  return 0;
}

Output:-

Enter three numbers: 10 30 12
Maximum number = 30.00

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *