0 0

What is Ternary Operator in C programming with example

- Advertisement -
Read Time:1 Minute, 21 Second
What is Ternary Operator in C programming with example
What is Ternary Operator in C programming with example

What is Ternary Operator in C programming with example

Ternary Operator

In the C language ternary operator is allowing for executing or running any code based on the given value to the condition, and the condition result value of the expression returned to the output. The important use of a ternary operator decreases the number of lines of codes and increases the performance of the application. Most of the research articles claimed that the expression result is faster than a statement result (conventional if-else condition). Ternary operator contains 3 expressions; Expression1, Expression2, and Expression3. Expression1 holds the condition to check, Expression2 will hold true value evaluated by Expression1 condition, and Expression3 will hold false value evaluated by Expression1 condition.

Advantages:

  • It reduces the code.
  • Improves performance.
  • Overcome conventional use of if and else condition always.

How does ternary operator work in C language?

C language ternary operator works based on the ternary operator(?), If the condition is evaluated true then it executes the true expression value at the left-hand side of the colon(:) symbol and if the condition is evaluated false then it executes false expression value at the right-hand side of the colon(:) symbol.

Syntax

Expression1?Expression2:Expression3;

Example

#include<stdio.h>
int main(){
 int age;

   printf("Enter age");
   scanf("%d", &age);
  
   age >= 18 ? printf("Adult") : printf("Not Adult");
   return 0;
}

Create your own website with hostinger:

Click on this image

TYPES OF INSTRUCTIONS IN C PROGRAMMING

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
- Advertisement -

Follow US!

FansLike
FollowersFollow
FollowersFollow
SubscribersSubscribe

Latest Post

Introduction to Online Analytical Processing and Data Mart

Introduction to Online Analytical Processing and Data Mart

Introduction to Online Analytical Processing and Data Mart Online Analytical Processing OLAP is computer processing that enables a user to easily and selectively extract and view...
A data warehouse is a relational database that is designed for query and analysis rather than for transaction processing

Introduction to Data Warehouse

Introduction to Data Warehouse A data warehouse is a relational database that is designed for query and analysis rather than for transaction processing. It usually...
Introduction to Advance Database System

Introduction to Advance Database System

Introduction to Advance Database System ABSTRACT Data is collection of raw facts and figure which is basic for decision making. Anything which is unorganized is called...
C program to check student is pass or fail using if else, nested if and ternary operator.

What is Conditional Statement in C programming

What is Conditional Statement in C programming Conditional Statement Conditional Statements in C programming are used to make decisions based on the different conditions. Conditional statements...
C programming if else statement

C programming if else statement

C programming if else statement Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements execute sequentially when there...
Types of instruction in C programming

Types of instructions in C programming

Types of instructions in C programming: C Instructions C instructions are the commands in the program that instructs the compiler to do certain action. Basically it...