0% found this document useful (0 votes)
10 views

C Ternary Operator (With Examples)

The document explains the use of the ternary operator in C programming, which allows for conditional execution of code based on a boolean expression. It provides syntax examples and demonstrates how to use the operator to determine if a person can vote based on age input. Additionally, it compares the ternary operator to traditional if...else statements, highlighting its conciseness in certain scenarios.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C Ternary Operator (With Examples)

The document explains the use of the ternary operator in C programming, which allows for conditional execution of code based on a boolean expression. It provides syntax examples and demonstrates how to use the operator to determine if a person can vote based on age input. Additionally, it compares the ternary operator to traditional if...else statements, highlighting its conciseness in certain scenarios.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?


36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com

C Ternary Operator
We use the ternary operator in C to run one code when
the condition is true and another code when the condition
is false. For example,

(age >= 18) ? printf("Can Vote") : printf("Cannot Vote");

Here, when the age is greater than or equal to 18,


Can Vote is printed. Otherwise, Cannot Vote is printed.

Syntax of Ternary Operator


The syntax of ternary operator is :

testCondition ? expression1 : expression 2;

The testCondition is a boolean expression that results in


either true or false. If the condition is

true - expression1 (before the colon) is executed

false - expression2 (after the colon) is executed


The ternary
Thank you for printing operator
our content takes 3 operandsPlease check back soon for new contents.
at www.domain-name.com.
(condition, expression1 and expression2) . Hence, the
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
name PRO!
with Programiz ternary operator.
utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Example: C Ternary Operator

#include <stdio.h>

int main() {
int age;

// take input from users


printf("Enter your age: ");
scanf("%d", &age);

// ternary operator to find if a person can vote or not


(age >= 18) ? printf("You can vote") : printf("You cann

return 0;
}

Run Code (/c-programming/online-compiler)

Output 1

Enter your age: 12


You cannot vote

In the above example, we have used a ternary operator


that checks whether a user can vote or not based on the
input value. Here,

age >= 18 - test condition that checks if input value is


greater or equal to 18

printf("You can vote") - expression1 that is executed


if condition is true
Thank you for printingprintf("You
our content atcannot vote") - expression2
www.domain-name.com. that isback soon for new contents.
Please check
executed if condition is false
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount banner&utm_campaign=programiz&utm_medium=referral)
Nowuser inputs 12,
Here, the so the condition becomes false.
(https://programiz.pro/learn/master-c-programming?
Programiz
Hence, weSearch
get You cannot& vote as output.
PRO
utm_source=nav-
tutorials examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com

Output 2

Enter your age: 24


You can vote

This time the input value is 24 which is greater than 18.


Hence, we get You can vote as output.

Assign the ternary operator to a variable


In C programming, we can also assign the expression of
the ternary operator to a variable. For example,

variable = condition ? expression1 : expression2;


Here,
Thank you for printing if content
our the testatcondition is true , expression1
www.domain-name.com. willback
Please check be soon for new contents.
assigned to the variable. Otherwise, expression2 will be
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
assigned.
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Let's see an example
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
#include <stdio.h>

int main() {

// create variables
char operator = '+';
int num1 = 8;
int num2 = 7;

// using variables in ternary operator


int result = (operator == '+') ? (num1 + num2) : (num1
printf("%d", result);

return 0;
}

// Output: 15

Run Code (/c-programming/online-compiler)

In the above example, the test condition


(operator == '+') will always be true. So, the first
expression before the colon i.e the summation of two
integers num1 and num2 is assigned to the result

variable.

And, finally the result variable is printed as an output


giving out the summation of 8 and 7. i.e 15.
Ternary
Thank you for printing Operator
our content Vs. if...elsePlease
at www.domain-name.com. Statement in for new contents.
check back soon

36%
C
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
off
with Programiz PRO! utm_source=sticky-
Claim Discount
In someNowof the cases, banner&utm_campaign=programiz&utm_medium=referral)
we can replace the if...else
(https://programiz.pro/learn/master-c-programming?
Programiz
statement with a tutorials
ternary & operator.
PRO
utm_source=nav-
Search examplesThis will make our code
(/)
floating&utm_campaign=programiz&utm_medium=referral)
cleaner and shorter.
www.domain-name.com

Let's see an example:

#include <stdio.h>

int main() {
int number = 3;

if (number % 2 == 0) {
printf("Even Number");
}
else {
printf("Odd Number");
}

return 0;
}

Run Code (/c-programming/online-compiler)

We can replace this code with the following code using


the ternary operator.

#include <stdio.h>

int main() {
int number = 3;

(number % 2 == 0) ? printf("Even Number") : printf("Odd

return 0;
}

Run Code (/c-programming/online-compiler)


Here,
Thank you for printing both
our theatprograms
content are doing thePlease
www.domain-name.com. samecheck
task,back soon for new contents.
checking even/odd numbers. However, the code using the
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
ternary
with Programiz operator looks
PRO! clean and concise.
utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
In such cases, where there is only one statement inside
Programiz
PRO
utm_source=nav-
Search tutorials & examples
the (/)
if...else block, we can replace it with a
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
ternary operator .

Ternary operator vs if…else

Share on:

(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/int
u=https://www.programiz.com/c- text=Check%20this%2
programming/ternary-operator) programming/ternary-o

Did you find this article helpful?


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?


36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com

Related Tutorials

C Tutorial

C Programming Operators

(/c-programming/c-operators)

C Tutorial

C if...else Statement

(/c-programming/c-if-else-statement)

C Tutorial

C switch Statement

(/c-programming/c-switch-case-statement)

C Tutorial

C for Loop

(/c-programming/c-for-loop)

You might also like