void sum(void) { unsigned int a = 6; int b = -10; if(a + b > 6) puts(">6"); else puts("<6"); }
.当混合不同类型的变量进行计算时,便可能会发生类型转换
相同类型的数据在转换时有规则可循:
字符必须先转换为整数(C语言规定字符类型数据和整型数据之间可以通用)
short型转换为int型(同属于整型)
float型数据在运算时一律转换为双精度(double)型,以提高运算精度(同属于实型)
赋值时,一律是右部值转换为左部类型
[注]
当整型数据和双精度数据进行运算时,C先将整型数据转换成双精度型数据,再进行运算,结果为双精度类型数据
当字符型数据和实型数据进行运算时,C先将字符型数据转换成实型数据,然后进行计算,结果为实型数据
所以要特别的注意类型的默认转换规则:
下面举行说明类型转换的规则。例如执行:
<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></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>