Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, July 11, 2020

Conditional or ternary operator in C

Conditional or ternary (?:) operator in C
By Ahmad Irshad | C Programming Tutorial > Conditional or ternary operaotr in C

Conditional or ternary Operator in C | Protec

The ternary operator (?:) is also known as conditional operator. It is a replacement of if..else statements. Which is used to evaluate an expression based on condition in place of longer if...else conditional statements.

Conditional/Ternary Operators in C | Protec

Syntax for ternary operator:

  1. <conditional-expression> ? <true-expression> : <false-expression>

1) The ternary operator takes three arguments as you can see above.
2) Syntax of ternary operator is always be like <conditional-expression> followed by ? symbol then <true-expression> followed by :
symbol and then at last <false-expression>
3) If the condition is true then <true-expression> is executed else <false-expression> is executed. Let's have a look at the below example to uderstand it better way.

Example 1: Ternary operator (?:)

  1. #include <stdio.h> int main() { int age; printf("Please enter your age here:\n"); scanf("%d", &age); /* * If condition is true then will be print " You are adult." * else will be print " You are not adult." */ (age >= 18) ? printf("You are adult") : printf("You are not adult"); return 0; }

Output:

  1. Please enter your age here: 15
  2. You are not adult

In the above program prints You are not adult.Since the condition (age >= 18) is false therefore, <false-expression> gets executed.


Example 2: Ternary operator (?:)

  1. #include <stdio.h> int main() { int num1, num2, max;
  2. printf("Please enter any two numbers");
  3. scanf("%d%d", &num1, &num2); /* * If (num > num2) then * assign num1 to max * else * assign num2 to max */ max = (num1 > num2) ? num1 : num2;
  4. printf("Maximum is %d.", max); return 0; }

Output:

  1. Please enter any two numbers : 10, 20
  2. Maximum is 20






Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us