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.
In C program Write a program that mimics a calculator. The program should prompt for two...
In C program Write a program that mimics a calculator. The program should prompt for two integers and the operation to be performed. It should then output the numbers, the operator, and the result. All the results should also be integers. For division, if the denominator is zero, output an appropriate message. Example (Numbers and symbols with underscore indicate an input): Enter integer +|-|*|/ integer: 3 + 2 3 + 2 = 5 -------------------------------------------- Enter integer +|-|*|/ integer: 13 *...
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.
(must be in C program) Write a program that reads triplets from an input file, into...
(must be in C program) Write a program that reads triplets from an input file, into a, operator, and b and then computes. The program must warn users if division by zero or an unknown operator. The operator is one of the four standard operators +, -, /, *. a and b are integers. For sample input file: 1 + 5 5 / 0 3 * 2 It must generate output: 1 + 5 = 6 5 / 0 =...
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
What logical operator in Matlab accepts one argument? A. and B. not C. or D .There...
What logical operator in Matlab accepts one argument? A. and B. not C. or D .There is no such operator in matlab