C语言中的三目运算符 ?: 的新用法
2014-04-10 16:01
274 查看
说明: 该运算符不仅能用在变量之间,竟然还可以用在函数之间,相当于可传参数的函数调用
1 void show_1(int val)
2 {
3 cout << "function show_1 called! and var is "<< var << endl;
4 }
5
6
7 void show_2(int val)
8 {
9 cout << "function show_2 called! and var is " << var << endl;
10 }
11
12
13 void quest_test()
14 {
15 int c = 0;
16 int a = 1, b = 2;
17 (c++ ? a : b)--;
18 cout << a << " and " << b << endl;
19 (c ? a : b)++;
20 cout << a << " and " << b << endl;
21 (c ? show_1 : show_2)(100);
22 }
23
24
25 最后的输出是
26 1 and 1
27 2 and 1
28 function show_1 called and var is 100!
其中:
(c ? show_1 : show_2)(100);
其实等同于
if (c)
show_1(100);
else
show_2(100);
另类函数调用,可作参考
1 void show_1(int val)
2 {
3 cout << "function show_1 called! and var is "<< var << endl;
4 }
5
6
7 void show_2(int val)
8 {
9 cout << "function show_2 called! and var is " << var << endl;
10 }
11
12
13 void quest_test()
14 {
15 int c = 0;
16 int a = 1, b = 2;
17 (c++ ? a : b)--;
18 cout << a << " and " << b << endl;
19 (c ? a : b)++;
20 cout << a << " and " << b << endl;
21 (c ? show_1 : show_2)(100);
22 }
23
24
25 最后的输出是
26 1 and 1
27 2 and 1
28 function show_1 called and var is 100!
其中:
(c ? show_1 : show_2)(100);
其实等同于
if (c)
show_1(100);
else
show_2(100);
另类函数调用,可作参考
相关文章推荐
- C语言中的所有运算符用法及总结
- 黑马程序员__C语言逻辑运算符和三目运算符
- C语言开发之运算符、表达式用法
- 讲通一个小知识点---C语言三目运算符
- c语言基本运算中的自增运算符和自减运算符以及三目运算符
- C语言学习笔记(三) 输入输出函数的基本用法以及运算符
- c语言中位运算符及用法
- # 的用法(c语言中) 字符串创建运算符
- C语言中三目运算符及其应用
- 补.从零开始学习C语言--运算符补充(自增/自减、三目、逗号)
- 三目运算符的用法
- C语言的三目运算符
- C语言中可变参数的用法
- c语言结构体用法
- vb中的三目运算符!
- C++基础:C语言中##的用法
- A【1-1-12】C语言运算符优先级与结合方向
- C语言结构体用法详解
- C语言中的errno(错误报告)用法
- c语言操作运算符的优先级,*p++,++*p,++*p++的结果