﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-静水流长-随笔分类-C</title><link>http://www.cppblog.com/LinusYu/category/14898.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 03 Oct 2010 13:20:18 GMT</lastBuildDate><pubDate>Sun, 03 Oct 2010 13:20:18 GMT</pubDate><ttl>60</ttl><item><title>C语言中的一些问题</title><link>http://www.cppblog.com/LinusYu/archive/2010/09/10/126352.html</link><dc:creator>LinusYu</dc:creator><author>LinusYu</author><pubDate>Fri, 10 Sep 2010 13:59:00 GMT</pubDate><guid>http://www.cppblog.com/LinusYu/archive/2010/09/10/126352.html</guid><wfw:comment>http://www.cppblog.com/LinusYu/comments/126352.html</wfw:comment><comments>http://www.cppblog.com/LinusYu/archive/2010/09/10/126352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LinusYu/comments/commentRss/126352.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LinusYu/services/trackbacks/126352.html</trackback:ping><description><![CDATA[<div style="TEXT-ALIGN: center">C语言中的符号重载
<table title=C语言中的符号重载 style="WIDTH: 800px; HEIGHT: 610px; TEXT-ALIGN: left" border=10>
    <tbody>
        <tr>
            <td>符号</td>
            <td class="" id="" vAlign=center align=left>意义</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>static</td>
            <td>在函数内部，表示该变量的值在各个调用间一直保持延续性<br>在函数这一级，表示该函数只对本文件可见</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>extern</td>
            <td>用于函数定义，表示全局可见（属于冗余的）<br>用于变量，表示它在其他地方定义</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>void</td>
            <td>作为函数的返回类型，表示不返回任何值<br>在指针声明中，表示通用指针的类型<br>位于参数列表中，表示没有参数</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>*</td>
            <td>乘法运算符<br>用于指针，间接引用<br>在声明中，表示指针</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>&amp;</td>
            <td>位的AND操作符<br>取地址操作符</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>=</td>
            <td>赋值操作符</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>==<br></td>
            <td>比较运算符</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>&lt;=<br>&lt;&lt;=</td>
            <td>小于等于运算符<br>左移复合赋值运算符</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>&lt;</td>
            <td>小于运算符<br>#include指令的左界定符</td>
        </tr>
        <tr>
            <td class="" id="" vAlign=top align=middle>()</td>
            <td>在函数定义中，包围形式参数表。<br>调用一个函数<br>改变表达式的运算次序<br>将值转换为其他类型（强制类型转换）<br>定义带参数的宏<br>包围sizeof操作符的操作数（如果它是类型名）</td>
        </tr>
    </tbody>
</table>
<div style="TEXT-ALIGN: center"><br></div>
<div style="TEXT-ALIGN: center">C语言运算符优先级存在的问题</div>
<div style="TEXT-ALIGN: center"><br>
<table style="TEXT-ALIGN: left" width=800 align=center border=10>
    <tbody>
        <tr>
            <td>优先级问题</td>
            <td>表达式</td>
            <td>人们可能误以为的结果</td>
            <td>实际结果</td>
        </tr>
        <tr>
            <td>.的优先级高于*<br>-&gt;操作符用于消除<br>这个问题</td>
            <td>*p.f</td>
            <td>p所指对象的字段f<br>(*p).f</td>
            <td>对p取f偏移，作为指针，<br>然后进行解除引用的操作<br>*(p.f)</td>
        </tr>
        <tr>
            <td>[]高于*</td>
            <td>int *ap[]</td>
            <td>ap是个指向int数组的指针<br>int(*ap)[]</td>
            <td>ap是个元素为int指针的数组<br>int *(ap[])</td>
        </tr>
        <tr>
            <td>函数()高于*</td>
            <td>int *fp()</td>
            <td>fp是个函数指针，所指函数<br>返回int。int(*fp)()</td>
            <td>fp是个函数，返回int*<br>int *(fp())</td>
        </tr>
        <tr>
            <td>==和!=高于位操作符</td>
            <td>(val &amp; mask != 0)</td>
            <td>(val &amp; mask) != 0</td>
            <td>val &amp; (mask != 0）</td>
        </tr>
        <tr>
            <td>==和!=高于赋值符</td>
            <td>c = getchar()!=EOF</td>
            <td>(c = getchar()) != EOF</td>
            <td>c = (getchar() != EOF)</td>
        </tr>
        <tr>
            <td>算术运算高于移位运算符</td>
            <td>msb &lt;&lt; 4 + lsb</td>
            <td>(msb &lt;&lt; 4) + lsb</td>
            <td>msb &lt;&lt; (4 + lsb)</td>
        </tr>
        <tr>
            <td>逗号运算符在所有运算符中<br>优先级最低</td>
            <td>i = 1, 2</td>
            <td>i = (1, 2)</td>
            <td>(i = 1), 2</td>
        </tr>
    </tbody>
</table>
</div>
</div>
<img src ="http://www.cppblog.com/LinusYu/aggbug/126352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LinusYu/" target="_blank">LinusYu</a> 2010-09-10 21:59 <a href="http://www.cppblog.com/LinusYu/archive/2010/09/10/126352.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于无符号类型的建议</title><link>http://www.cppblog.com/LinusYu/archive/2010/09/10/126348.html</link><dc:creator>LinusYu</dc:creator><author>LinusYu</author><pubDate>Fri, 10 Sep 2010 12:24:00 GMT</pubDate><guid>http://www.cppblog.com/LinusYu/archive/2010/09/10/126348.html</guid><wfw:comment>http://www.cppblog.com/LinusYu/comments/126348.html</wfw:comment><comments>http://www.cppblog.com/LinusYu/archive/2010/09/10/126348.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LinusYu/comments/commentRss/126348.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LinusYu/services/trackbacks/126348.html</trackback:ping><description><![CDATA[（摘自C专家编程）
<div>
<div>&nbsp;&nbsp; &nbsp;字符和整型（整型升级）</div>
<div>&nbsp;&nbsp; &nbsp;char，short int或者int型位段（bit-field），包括它们的有符号或无符号类型，以及枚举类型，可以使用在需要int或unsigned int的表达式中。如果int可以完整表示源类型的所有值(即int是32位，译者注），那么该源类型的值就转换为int，否则转换为unsigned int。这称为整型升级。</div>
<div>&nbsp;&nbsp; &nbsp;寻常算术转换</div>
<div>&nbsp;&nbsp; &nbsp;许多操作数类型为算术类型的双目运算符会引发转换，并以类似的方式产生结果类型。它的目的是产生一个普通类型，同时也是运算结果的类型，这个模式称为&#8220;寻常算术转换&#8220;。</div>
<div>&nbsp;&nbsp; &nbsp;首先，如果其中一个操作数是long double，那么另一个操作数也被转换为long double。其次，如果其中一个操作数的类型是double，那么另一个操作数也被转换为double。再次，如果其中一个操作数的类型是float，那么另一个操作数也被转换为float。否则，两个操作数执行整型升级，执行下面的规则：</div>
<div>&nbsp;&nbsp; &nbsp;如果其中一个操作数的类型是unsigned long int，那么另一个操作数也被转换为unsigned long int。其次，如果其中一个操作数的类型是long int，而另一个操作数的类型是unsigned int，如果long int能够完整表示unsigned int的所有值（即long是32位而int是16位，译者注），那么unsigned int类型操作数被转换为long int，如果long int不能完整表示unsigned int的所有值（即long和int均为32位，译者注），那么两个操作数都被转换为unsigned long int。再次，如果其中一个操作数是long int，那么另一个操作数被转换为long int。再再次，如果其中一个操作数的类型是unsigned int，那么另一个操作数被转换为unsigned int。如果所有以上情况都不属于，那么两个操作数都为int。</div>
<div>&nbsp;&nbsp; &nbsp;通俗的说（当然存在漏洞，而且不够精确）：当执行算术运算时，操作数的类型如果不同，就会发生转换。数据类型一般朝着浮点精度更高、长度更长的方向转换，整型数如果转换为signed不会丢失信息，就转换为signed，否则转换为unsigned。</div>
<div>&nbsp;&nbsp; &nbsp;ANSI C标准采用值保留（value preserving）原则，就是当把几个整型操作数混合使用，结果类型有可能是有符号数，也有可能是无符号数，取决于操作数的类型的相对大小。</div>
<div>对无符号类型的建议：</div>
<div>&nbsp;&nbsp; &nbsp;尽量不要在代码中使用无符号类型，以免增加不必要的复杂性。尤其是，不要仅仅因为无符号数不存在赋值（如年龄、国债）而用它来表示数量。</div>
<div>&nbsp;&nbsp; &nbsp;尽量使用像int那样的有符号类型，这样在涉及升级混合类型的复杂细节时，不必担心边界情况（如-1被翻译成非常大的正数）。</div>
<div>&nbsp;&nbsp; &nbsp;只有在使用位段和二进制掩码时，才可以使用无符号数。应该在表达式中使用强制类型转换，使操作数均为有符号数或者无符号数，这样就不必由编译器来选择结果的类型。</div>
</div>
<img src ="http://www.cppblog.com/LinusYu/aggbug/126348.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LinusYu/" target="_blank">LinusYu</a> 2010-09-10 20:24 <a href="http://www.cppblog.com/LinusYu/archive/2010/09/10/126348.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>