Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Ternary Operator | Control Statements
C Basics

bookTernary Operator

メニューを表示するにはスワイプしてください

The Ternary Operator in C

In C, there's a shorthand for the if-else statement known as the ternary operator, represented by ?:.

(condition) ? value_if_true : value_if_false

You can use this operator when you want to assign one of two values to a variable based on a condition. For instance, to determine the larger of two variables:

int a = 10; 
int b = 4;
int c;
	
c = (a > b) ? a : b;

After executing the above statement, what will be the value of c?

For comparison, here's how the same logic looks using the if...else statement:

if (a > b)
{
	c = a;
}
else
{
	c = b;
}

Note

While the ternary operator is a concise way to express conditionals, it's best to avoid it in complex structures. It can make the code harder to read for your teammates (though perhaps not for your instructor).

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  3
some-alt