C语言隐式类型转换规则

C语言规定,不同类型的数据需要转换成同一类型后才可进行计算,在整型、实型和字符型数据之间通过类型转换便可以进行混合运算(但不是所有类型之间都可以进行转换)

.当混合不同类型的变量进行计算时,便可能会发生类型转换
相同类型的数据在转换时有规则可循:
字符必须先转换为整数(C语言规定字符类型数据和整型数据之间可以通用)
short型转换为int型(同属于整型)
float型数据在运算时一律转换为双精度(double)型,以提高运算精度(同属于实型)
赋值时,一律是右部值转换为左部类型
[注]
当整型数据和双精度数据进行运算时,C先将整型数据转换成双精度型数据,再进行运算,结果为双精度类型数据
当字符型数据和实型数据进行运算时,C先将字符型数据转换成实型数据,然后进行计算,结果为实型数据


2.4<wbr>数据类型转换<span style="font-size:16px; line-height:1.5em">在<span style="font-family:Times; font-size:16px; line-height:1.5em">C</span><wbr>语言的表达式中,准许对不同类型的数值型数据进行某一操作或混合运算。当不同类型的数据进行操作时,应当首先将其转换成相同的数据类型,然后进行操作。数据类型转换有两种形式,即隐式类型转换和显示类型转换。</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">2.4.1</span><wbr>隐式类型转换</wbr></span><wbr><span style="font-size:16px; line-height:1.5em">所谓隐式类型转换就是在编译时由编译程序按照一定规则自动完成,而不需人为干预。因此,在表达式中如果有不同类型的数据参与同一运算时,编译器就在编译时自动按照规定的规则将其转换为相同的数据类型。</span><wbr><br><span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">C</span><wbr>语言规定的转换规则是由低级向高级转换。例如,如果一个操作符带有两个类型不同的操作数时,那么在操作之前行先将较低的类型转换为较高的类型,然后进行运算,运算结果是较高的类型。更确切地说,对于每一个算术运算符,则遵循图<span style="font-family:Times; font-size:16px; line-height:1.5em">2-2</span><wbr>所示的规则。</wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em"><wbr><a href="http://image.17xie.com/i/348/200708221187748850890.jpg" target="_blank" style="text-decoration:none"><img height="271" src="http://image.17xie.com/i/348/200708221187748850890.jpg" width="378" alt="" style="border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial"></a><wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">图<span style="font-family:宋体; font-size:16px; line-height:1.5em">2-2</span><wbr>数据类型转换规则之一</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">注意:在表达式中,所有的<span style="font-family:Times; font-size:16px; line-height:1.5em">float</span><wbr>类型都转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>型以提高运算精度。</wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">在赋值语句中,如果赋值号左右两端的类型不同,则将赋值号右边的值转换为赋值号左边的类型,其结果类型还是左边类型。</span><wbr><br><span style="font-size:16px; line-height:1.5em">因为函数参数是表达式,因此,当参数传递给函数时,也发生类型转换。具体地说,<span style="font-family:Times; font-size:16px; line-height:1.5em">char</span><wbr>和<span style="font-family:Times; font-size:16px; line-height:1.5em">short</span><wbr>均转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>;<span style="font-family:Times; font-size:16px; line-height:1.5em">float</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。这就是为什么我们把函数参数说明为<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>和<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,尽管调用函数时用<span style="font-family:Times; font-size:16px; line-height:1.5em">char</span><wbr>和<span style="font-family:Times; font-size:16px; line-height:1.5em">float .</span><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">也可以将图<span style="font-family:Times; font-size:16px; line-height:1.5em">2-2</span><wbr>所示的规则用图<span style="font-family:Times; font-size:16px; line-height:1.5em">2-3</span><wbr>表示。图<span style="font-family:Times; font-size:16px; line-height:1.5em">2-3</span><wbr>中的水平箭头表示必定转换,纵向箭头表示两个操作对象类型不同时的转换方向。</wbr></wbr></wbr></span><wbr><br><wbr><a href="http://image.17xie.com/i/348/200708221187748917843.jpg" target="_blank" style="text-decoration:none"><img height="94" src="http://image.17xie.com/i/348/200708221187748917843.jpg" width="378" alt="" style="border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial"></a><wbr><br><span style="font-size:16px; line-height:1.5em">图<span style="font-family:宋体; font-size:16px; line-height:1.5em">2-3</span><wbr>数据类型转换规则之二</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">下面举行说明类型转换的规则。例如执行:</span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">x=100+'a'+1.5 * u+f/'b'-s * 3.1415926</span><wbr><br><span style="font-size:16px; line-height:1.5em">其中,<span style="font-family:Times; font-size:16px; line-height:1.5em">u</span><wbr>为<span style="font-family:Times; font-size:16px; line-height:1.5em">unsigned</span><wbr>型,<span style="font-family:Times; font-size:16px; line-height:1.5em">f</span><wbr>为<span style="font-family:Times; font-size:16px; line-height:1.5em">float</span><wbr>型,<span style="font-family:Times; font-size:16px; line-height:1.5em">s</span><wbr>为<span style="font-family:Times; font-size:16px; line-height:1.5em">short</span><wbr>型,<span style="font-family:Times; font-size:16px; line-height:1.5em">x</span><wbr>为<span style="font-family:Times; font-size:16px; line-height:1.5em">float</span><wbr>型。式中右面表达式按如下步骤处理:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">1</span><wbr>)首先将<span style="font-family:Times; font-size:16px; line-height:1.5em">'a'</span><wbr>、<span style="font-family:Times; font-size:16px; line-height:1.5em">'b'</span><wbr>和<span style="font-family:Times; font-size:16px; line-height:1.5em">s</span><wbr>换成<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>,将<span style="font-family:Times; font-size:16px; line-height:1.5em">1.5</span><wbr>和<span style="font-family:Times; font-size:16px; line-height:1.5em">f</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>型。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">2</span><wbr>)计算<span style="font-family:Times; font-size:16px; line-height:1.5em">100+'a'</span><wbr>,因<span style="font-family:Times; font-size:16px; line-height:1.5em">'a'</span><wbr>已转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>型,于是此运算结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">197</span><wbr>。</wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">3</span><wbr>)计算<span style="font-family:Times; font-size:16px; line-height:1.5em">1.5*u</span><wbr>,由于<span style="font-family:Times; font-size:16px; line-height:1.5em">1.5</span><wbr>已转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">u</span><wbr>是<span style="font-family:Times; font-size:16px; line-height:1.5em">unsigned</span><wbr>型,于是首先<span style="font-family:Times; font-size:16px; line-height:1.5em">u</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,然后进行运算,运算结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">4</span><wbr>)计算<span style="font-family:Times; font-size:16px; line-height:1.5em">197+1.5 * u</span><wbr>,先将<span style="font-family:Times; font-size:16px; line-height:1.5em">197</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>(如<span style="font-family:Times; font-size:16px; line-height:1.5em">197.00</span><wbr>…<span style="font-family:Times; font-size:16px; line-height:1.5em">00</span><wbr>),其结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">5</span><wbr>)计算<span style="font-family:Times; font-size:16px; line-height:1.5em">f/ 'b'</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">f</span><wbr>已转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">'b'</span><wbr>已转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>,于是先将<span style="font-family:Times; font-size:16px; line-height:1.5em">'b'</span><wbr>再转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,其结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">6</span><wbr>)计算(<span style="font-family:Times; font-size:16px; line-height:1.5em">197+1.5 * u</span><wbr>)<span style="font-family:Times; font-size:16px; line-height:1.5em">+f / 'b'</span><wbr>,者均为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,于是结果也为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">7</span><wbr>)计算<span style="font-family:Times; font-size:16px; line-height:1.5em">s * 3.1415926</span><wbr>,先将<span style="font-family:Times; font-size:16px; line-height:1.5em">s</span><wbr>由<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>,然后进行运算,其结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">8</span><wbr>)最后与前面得的结果相减,结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>。</wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">(<span style="font-family:Times; font-size:16px; line-height:1.5em">9</span><wbr>)最后将表达式的结果转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">float</span><wbr>并赋给<span style="font-family:Times; font-size:16px; line-height:1.5em">x</span><wbr>。</wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">2.4.2</span><wbr>显式类型转换</wbr></span><wbr><span style="font-size:16px; line-height:1.5em">显示类型转换又叫强制类型转换,它不是按照前面所述的转换规则进行转换,而是直接将某数据转换成指定的类型。这可在很多情况下简化转换。例如,</span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">int i</span><wbr>;<br> …<br><span style="font-family:Times; font-size:16px; line-height:1.5em">i=i+9.801</span><wbr><br><span style="font-size:16px; line-height:1.5em">按照隐式处理方式,在处理<span style="font-family:Times; font-size:16px; line-height:1.5em">i=i+9.801</span><wbr>时,首先<span style="font-family:Times; font-size:16px; line-height:1.5em">i</span><wbr>转换为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>型,然后进行相加,结果为<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>型,再将<span style="font-family:Times; font-size:16px; line-height:1.5em">double</span><wbr>型转换为整型赋给<span style="font-family:Times; font-size:16px; line-height:1.5em">i</span><wbr>。</wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">int i</span><wbr>;<br> …<br><span style="font-family:Times; font-size:16px; line-height:1.5em">i=i+</span><wbr>(<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>)<span style="font-family:Times; font-size:16px; line-height:1.5em">9.801</span><wbr><br><span style="font-size:16px; line-height:1.5em">这时直接将<span style="font-family:Times; font-size:16px; line-height:1.5em">9.801</span><wbr>转换成整型,然后与<span style="font-family:Times; font-size:16px; line-height:1.5em">i</span><wbr>相加,再把结果赋给<span style="font-family:Times; font-size:16px; line-height:1.5em">i</span><wbr>。这样可把二次转换简化为一次转换。</wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">显示类型转换的方法是在被转换对象(或表达式)前加类型标识符,其格式是:</span><wbr><br> (类型标识符)表达式<br><span style="font-size:16px; line-height:1.5em">例如,有如下程序段:</span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">main</span><wbr>()<br> {<br><span style="font-family:Times; font-size:16px; line-height:1.5em">int a</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">b</span><wbr>;<br><span style="font-family:Times; font-size:16px; line-height:1.5em">float c</span><wbr>;<br><span style="font-family:Times; font-size:16px; line-height:1.5em">b=a+int</span><wbr>(<span style="font-family:Times; font-size:16px; line-height:1.5em">c</span><wbr>);<br><span style="font-family:Times; font-size:16px; line-height:1.5em">printf</span><wbr>(<span style="font-family:Times; font-size:16px; line-height:1.5em">"b=d% \ n"</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">b</span><wbr>);<br> }<br><span style="font-size:16px; line-height:1.5em">在上述程序的运行过程中,在执行语句<span style="font-family:Times; font-size:16px; line-height:1.5em">b=a+int</span><wbr>(<span style="font-family:Times; font-size:16px; line-height:1.5em">c</span><wbr>)时,将<span style="font-family:Times; font-size:16px; line-height:1.5em">c</span><wbr>的值临时强制性转化为<span style="font-family:Times; font-size:16px; line-height:1.5em">int</span><wbr>型,但变量<span style="font-family:Times; font-size:16px; line-height:1.5em">c</span><wbr>在系统中仍为实型变量,这一点很重要,不少初学者在这个问题上忽略了这个问题。</wbr></wbr></wbr></wbr></wbr></span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">2.5</span><wbr>运算符和表达式<span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">2.5.1</span><wbr>运算符和表达式概述</wbr></span><wbr><span style="font-family:Times; font-size:16px; line-height:1.5em">1</span><wbr>.表达式<span style="font-size:16px; line-height:1.5em">一个表达式包含一个或多个操作,操作的对象称作运算元(或叫作操作数),而操作本身通过运算符体现的。例如<span style="font-family:Times; font-size:16px; line-height:1.5em">a</span><wbr>、<span style="font-family:Times; font-size:16px; line-height:1.5em">a-b</span><wbr>、<span style="font-family:Times; font-size:16px; line-height:1.5em">c=9.801</span><wbr>等都是一个表达式。</wbr></wbr></wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">一个表达式完成一个或多个操作,最终得到一个结果,而结果的数据类型由参加运算的操作决定。最简单的表达式是只含一个常量或变量的表达式,即只含一个操作数而不含运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">C</span><wbr>语言中表达式的种类十分丰富,主要有如下一些:</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">算术表达式:进行一般的计算。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">赋值表达式:进行赋值操作。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">关系表达式:进行比较判断。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">逻辑表达式:进行逻辑比较判断。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">条件表达式:进行条件满足与否的判断。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">逗号表达式:实际上是一种复杂运算,可以包含多个算术表达式。</span><wbr><br><span style="font-family:Times; font-size:16px; line-height:1.5em">2</span><wbr>.<span style="font-family:Times; font-size:16px; line-height:1.5em">C</span><wbr>语言的操作符<span style="font-size:16px; line-height:1.5em"><span style="font-family:Times; font-size:16px; line-height:1.5em">C</span><wbr>语言的特点之一是具有丰富和使用灵活的运算符,概括起来它有如下的几类运算符:</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">算术运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">赋值运算符(包括符合赋值运算符)。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">关系运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">逻辑运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">条件运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">逗号运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">位运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">指针运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">求字节运算符(可以归并到函数的应用中去,它是通过函数<span style="font-family:Times; font-size:16px; line-height:1.5em">sizeof</span><wbr>()来进行运算的)。</wbr></span><wbr><br><span style="font-size:16px; line-height:1.5em">n</span><wbr><span style="font-size:16px; line-height:1.5em">强制类型转换运算符。</span><wbr><br><span style="font-size:16px; line-height:1.5em">这些运算符如表<span style="font-family:Times; font-size:16px; line-height:1.5em">2-4</span><wbr>所示。</wbr></span><wbr><br><span style="font-size:13px; line-height:1.5em">表<span style="font-family:Times; font-size:16px; line-height:1.5em">2-4 C</span><wbr>语言中的运算符</wbr></span><wbr><br><center> <strong><wbr>名称</wbr></strong><wbr></wbr> </center> <wbr><center> <strong><wbr>操作符</wbr></strong><wbr></wbr> </center> <wbr>自增,自减<br><span style="font-family:Times; font-size:16px; line-height:1.5em">+ +</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">- -</span><wbr><br> 逻辑与、或、非<br> &&,︱︱,!<br><span style="font-size:13px; line-height:1.5em">续表<span style="font-family:Times; font-size:16px; line-height:1.5em">2</span><wbr>-<span style="font-family:Times; font-size:16px; line-height:1.5em">4</span><wbr></wbr></wbr></span><wbr><br><center> <strong><wbr>名称</wbr></strong><wbr></wbr> </center> <wbr><center> <strong><wbr>操作符</wbr></strong><wbr></wbr> </center> <wbr>指针操作及引用<br><span style="font-family:Times; font-size:16px; line-height:1.5em">*</span><wbr>,&<br> 加、减、乘、除、求模运算<br><span style="font-family:Times; font-size:16px; line-height:1.5em">+</span><wbr>,-,<span style="font-family:Times; font-size:16px; line-height:1.5em">*</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">/</span><wbr>,<span style="font-family:Times; font-size:16px; line-height:1.5em">%</span><wbr><br> 关系操作符<br><span style="font-family:Times; font-size:16px; line-height:1.5em">&lt;,&lt;=,&gt;,&gt;=,= =,! =</span><wbr><br> 按位与、或、异或、求反<br><span style="font-family:Times; font-size:16px; line-height:1.5em">&amp;,</span><wbr>丨<span style="font-family:Times; font-size:16px; line-height:1.5em">,^,~</span><wbr><br> 逗号表达式<br> ,<br> 类型转换<br> ()<br> 移位运算<br><span style="font-family:Times; font-size:16px; line-height:1.5em">&lt; &lt;,&gt; &gt;</span><wbr><br> 条件运算<br> ?:<br> 求占用的字节数<br><span style="font-family:Times; font-size:16px; line-height:1.5em">sizeof</span><wbr><br> 赋值<br><span style="font-family:Times; font-size:16px; line-height:1.5em">=,+ =,</span><wbr>-<span style="font-family:Times; font-size:16px; line-height:1.5em">=,*=,/ =,% =</span><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
先看程序:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char**argv){
unsigned int right = 1;
char left = -1;
if(left < right)printf("%d < %d\n", left, right);
else if(left == right)printf("%d = %d\n", left, right);
else printf("%d > %d\n", left, right);
system("PAUSE");
return 0;
}
运行结果是:-1 > 1
解释:按步骤进行.
1. 如果其中一个操作数为long double类型,则另一个操作数被转换为long double.
2. 否则,如果其中一个操作数为double, 则另一个操作数被转换为double.
3. 否则,如果其中一个操作数为float, 则另一个操作数也转换为float.
4. 否则,两个操作数进行 "整型升级":
a. 如果其中一个操作数为unsigned long int, 则另一个操作数也被视为unsigned long int.
b. 否则,如果其中一个操作数为long int,而另一个操作数类型是unsigned int, 并且long int能够表示unsigned int的所有值,则另一个操作数也被视为long int;如果long int不能表示unsigned int的所有值,则两个数都被视为unsigned long int.
c. 否则, 如果其中一个操作数是long int,则另一个操作数也被视为long int.
d. 否则, 如果其中一个操作数是unsigned int, 则另一个操作数也被视为unsigned int.
e. 否则, 两个操作数都被视为int.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值