三目运算符又称:三元运算符、条件运算符,用于根据二元条件的结果执行代码,可以有效的减少行代码的数量并提高应用程序的性能。

c语言中三目运算符
三目运算符具有以下形式:
表达1?表达2:表达3
说明:表达式1代表条件,表达式2和表达式3将是值或变量或语句或任何数学表达式。如果表达式1的结果为true,则表达式2将被执行,否则将执行表达式3。
例:
a <p>如果(a </p><p>流程图:</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/3889" title="TabTab"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/178599583454727.png" alt="TabTab" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/3889" title="TabTab" class="overflowclass">TabTab</a>
<p class="overflowclass">TabTab是一款AI智能体工具,首个全链路 Data Agent。</p>
</div>
<a rel="nofollow" href="/ai/3889" title="TabTab" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p style="text-align: center;"><img src="https://img.php.cn/upload/image/758/928/569/1550826365878270.jpg?x-oss-process=image/resize,p_40" title="1550826365878270.jpg" alt="2.jpg"></p><p>说明:三目运算符会将二进制条件作为输入,这使得它类似于“if-else”控制流程块。但是,它也返回一个值,其行为类似于函数。</p><p><span style="max-width:90%"><strong>三目运算符的使用示例</strong></span></p><pre class="brush:js;toolbar:false">#include<stdio.h>
int main() {
int a, b, max;
printf("输入任意2个数字: \n");
scanf("%d%d", & a, & b);
/* Following statement replaces the whole if-else statement and makes the code more concise*/
max = (a > b) ? a : b;
printf("最大数字是:%d", max);
return 0;
}</stdio.h>
输出:

以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。更多精彩内容大家可以关注php中文网相关教程栏目!!!
13万字C语言保姆级教程(深入):立即使用
在学习笔记中,你将探索c语言的核心概念和高级技巧!










