隐世转换 http://www.nowcoder.com/profile/380772/test/2198782/2922?onlyWrong=0
来源:互联网 发布:ubuntu更改文件夹权限 编辑:程序博客网 时间:2024/04/29 13:18
int
i = -
1
;
unsigned j =
1
;
if
(j > i)
printf(
" (j>i)成立\n"
);
else
printf(
" (j>i)不成立\n"
);
if
(i < j)
printf(
" (i<j)成立\n"
);
else
printf(
" (i<j)不成立\n"
);
正确答案: D 你的答案: D (正确)
(j>i)成立,(i<j)成立
(j>i)不成立,(i<j)成立
(j>i)成立,(i<j)不成立
(j>i)不成立,(i<j)不成立
- 添加笔记
- 收藏
- 纠错
本题知识点
参考解析添加解析
小海豹
答案是D。解释:表达式会包含隐式类型转换,它由编译器自动执行,不需程序员介入。何时发生隐式类型转换
1. 在混合类型的表达式中,操作数会被转换为相同类型
int ival; double dval;ival >= dval; // ival converted to double
2. 条件表达式会被转换为bool类型。
int ival; if (ival) // ival converted to bool while (cin) // cin converted to bool
条件操作符(? :)中的第一个操作数,逻辑非(!)、逻辑与(&&)、逻辑或(||)的操作数都是条件表达式。if、while、do while、以及for的第2个表达式都是条件表达式。
3. 初始化和赋值
int ival = 3.14 // 3.14 converted to int int *ip;ip = 0; // the int 0 converted to a null pointer of type int *
4. 在函数调用时,所传递的参数也可能发生隐式类型转换。
如何转换
1. 算术转换
算术转换保证在执行操作前,将二元操作符的两个操作数转换为同一类型,并使表达式的值也具有相同的类型。算术转换通常的是做整形提升(integral promotion),对于所有比int小的整形,包括char、signed char、unsigned char、short和unsigned short,如果该类型的所有可能的值都能包含在int内,它们就会被提升为int,否则被提升为unsigned int。如果将bool值提升为int,则false转换为0,true转换为1。包含short和int类型的表达式,short转换为int。如果int足以表示所有unsigned short类型的值,则将unsigned short转换为int,否则两个操作数均转换为unsigned int。long和unsigned int的转换也一样。只要机器上的long足够表示unsigned int类型所有的值,就将unsigned int转换为long,否则两个操作数都转换为unsigned long。在32位的机器上,long和int通常用一个字长表示,此时如果表达式包含unsigned int和long,两者都转换为unsigned long。如果表达式包含signed和unsigned int,signed会被转换为unsigned。如果int 操作数的值恰为负数,其转换为unsigned int可能会变为一个很大的正数(转换结果是该负值对unsigned int的取值个数求模)。所以最好避免对int和unsigned int的两个操作数进行比较。
转换示例:
2. 其他隐式转换
(1)数组名转换为指向其第一个元素的指针
int ia[10]; // array of 10 ints int *ip = ia; // convert ia to pointer to first element
另外,任意数据类型的指针都可转换为void *,整形数值常量0可以转换为任意类型指针。
(2)指针值可转换为bool
如果指针为0,转换为false,否则转换为true。
if (cp) // true if pointer cp is not zero
(3)算术类型与bool的转换
算术类型转换为bool时,0转换为false,其他值(包括负值)转换为true。将bool转换为算术类型时,true转换为1,false转换为0。
(4)转换与枚举类型
枚举类型对象或枚举成员将自动转换为整形,其转换结果可以用于任何需要使用整数值的地方。具体会被转换为哪种整形,依赖于枚举成员的最大值和机器。enum对象或枚举成员至少提升为int,如果int无法表示枚举成员的最大值,则提升到能表示所有枚举成员值的、大于int型的最小类型(unsigned int 、long或unsigned long)。
编辑于 2015-02-06 15:16:14
- 隐世转换 http://www.nowcoder.com/profile/380772/test/2198782/2922?onlyWrong=0
- 牛客网 http://www.nowcoder.com/profile/380772/test/2198844/25222?onlyWrong=0
- 牛客网 鼠标事件 http://www.nowcoder.com/profile/380772/test/2198844/16026?onlyWrong=0
- 牛客网 debug-release http://www.nowcoder.com/profile/380772/test/2198844/36245?onlyWrong=0
- 常见文件系统 系统函数 http://www.nowcoder.com/profile/380772/test/2198787/25355?onlyWrong=0
- 拷贝构造函数 http://www.nowcoder.com/profile/380772/test/2198787/36698?onlyWrong=0
- 重载运算符 http://www.nowcoder.com/profile/380772/test/2198787/2593?onlyWrong=0
- C++的不同数据类型值的比较语句 http://www.nowcoder.com/profile/380772/test/2198830/25642?onlyWrong=0
- std::vector::iterator重载了下面哪些运算符 http://www.nowcoder.com/profile/380772/test/2198787/1141?onlyWrong=0
- 减号的优先级http://www.nowcoder.com/profile/380772/test/2198787/873#summary
- 牛客网 http://www.nowcoder.com/test/question/done?tid=2198842&qid=14753#summary
- c++ 初始化 http://www.nowcoder.com/test/question/done?tid=2198842&qid=25454#summary
- 多线程 http://www.nowcoder.com/test/question/done?tid=2198842&qid=3939#summary
- const修饰的常量值 http://www.nowcoder.com/test/question/done?tid=2198842&qid=15952#summary
- 迭代器的问题 http://www.nowcoder.com/test/question/done?tid=2198842&qid=5962#summary
- 位域http://www.nowcoder.com/questionTerminal/853a980f7db14eec87209c6a1e2d99dd
- http://www.renren.com/profile.do?id=220869953
- http://www.blogmura.com/profile/170536.html
- iOS UITableView(七) cell删除总结
- storyboard中Modal 的使用
- 2016锻炼身体执行情况记录
- android colors.xml的用法
- 面向对象
- 隐世转换 http://www.nowcoder.com/profile/380772/test/2198782/2922?onlyWrong=0
- lvs、haproxy、nginx 负载均衡的比较分析
- Learning Python ~Ch4: Introducing Python Object Types (上) ~重点整理
- isKindOfClass和isMemberOfClass 的比较
- Objective-c:isKindOfClass 与 isMemberOfClass
- SQL 加序号(行号)
- nginx服务器安装及配置文件详解
- leetcode111---Minimum Depth of Binary Tree
- 32C3之PWN题目Readme的解法