Question

C program: What is a ternary operator? When should it be used and when should it...

C program: What is a ternary operator? When should it be used and when should it be avoided?

Homework Answers

Answer #1

Ternar operators is a operator in which we takes three arguments where first is condition, second is expression which is true and third is expression is false. This is replacement of if else statement.

Syntax:

condition ? expression true : expression false

Ternary operator is used when we want to reduce the lines of code by simply taking three arguments.

Example:

The required code is in C language:

#include <stdio.h>

int main()
{
int a = 8, b = 10, c;

c = (a < b) ? a : b;

printf("%d", c);

return 0;
}

Ternary operator should be avoided when there is a more than 2 condtions.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Describe cost-benefits analysis and explain when is should be used and when it should be avoided.
Describe cost-benefits analysis and explain when is should be used and when it should be avoided.
MATLAB What operator is used to "and" two logical arrays together? What MATLAB command is used...
MATLAB What operator is used to "and" two logical arrays together? What MATLAB command is used to empty the workspace to create a clean slate? What operator is used to "or" two logical arrays together?
Construct a C program which computes the minimum number of bills and coins needed to make...
Construct a C program which computes the minimum number of bills and coins needed to make change for a particular purchase. The cost of the item is $21.17 and the amount tendered is $100.00. These values should be built into your program using assignment statements rather than input into the program during program runtime. Your program should indicate how many bills and coins of each denomintaion are needed for the change. You should make use of the following denominations: Bills:...
Write a program that mimics a calculator. The program should take as input two integer and...
Write a program that mimics a calculator. The program should take as input two integer and an arithmetic operation (+, -, *, /, %) to be performed. It should then output the numbers the operator and the result. Division by zero??
Write a C++ program reads the grades. Declare a variable of type ifstream which is used...
Write a C++ program reads the grades. Declare a variable of type ifstream which is used to input a stream from a file: ifstream input; // input is the name of the variable Use the input operator (>>) to read data from the above file: input >> score1 >> score2 << score3; //
1) We should all be accustomed to the PEMDAS operator precedence rules (Parenthesis, exponentials, multiply, modulus,...
1) We should all be accustomed to the PEMDAS operator precedence rules (Parenthesis, exponentials, multiply, modulus, divide, addition, and subtraction) for constant integer expressions. For example, (3 + 4) X (6 -1) = 35 Please describe in your own words how the + and - symbols are used in ways other than for addition and subtraction. Describe what happens to the operator precedence rules when you use the + and - symbols in that manner.
When should a beginning balance be used for a new account, and what date should be...
When should a beginning balance be used for a new account, and what date should be used for the beginning balance? using QuickBooks
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
USE C++ Write a sum() function that is used to find the sum of the array...
USE C++ Write a sum() function that is used to find the sum of the array through pointers. In this program we make use of * operator. The * (asterisk) operator denotes the value of variable. Input: array = 2, 4, -6, 5, 8, -1 Output: sum = 12
for c language What function is used to deallocate dynamic memory when it is no longer...
for c language What function is used to deallocate dynamic memory when it is no longer required by your program? In your answer give the name of the function and its syntax, and how the C runtime system knows how much memory to deallocate. Give an appropriate example showing typical usage of this function