Ternary Operator in C Language - Codzify Tutorials

1 min 55 sec read Basic


What are Conditional Operators ?

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. It is very easy to understand Ternary Operator in C . Consider the following syntax below for better understanding,

Syntax :-

 expression 1 ? expression 2 : expression 3 

What this expression says is: "if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3"

It's equivalent if statement will look like this

 if(expression 1)
    {
    expression 2;
    }
    else
    {
    expression 3;
    }

Example :-

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
int a , b;

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

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

Output :-

Value of b is 30
Value of b is 20

Short Description of Example :-

(a == 1) ? 20: 30

Firtsly a == 1 is being evaluated but since a=10 condition becomes false so output will be 30.

(a == 10) ? 20: 30

Firtsly a == 10 is being evaluated but since a=10 condition becomes true so output will be 20 :)

It's equivalent if statement will look like this

  a = 10;
  if(a == 1 ) 
      printf("20");       --Just for understanding 
  else
      printf("30") ; 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials