﻿<?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>IT博客-依睛(IT blog) 我回来了，PHP&lt;--&gt;C/C++ LINUX-随笔分类-精巧小例子(谁都知)</title><link>http://www.cnitblog.com/guopingleee/category/5991.html</link><description>笨鸟
</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 20:01:01 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 20:01:01 GMT</pubDate><ttl>60</ttl><item><title>精巧小例子(一种风格)</title><link>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32375.html</link><dc:creator>向左向右走</dc:creator><author>向左向右走</author><pubDate>Fri, 24 Aug 2007 03:18:00 GMT</pubDate><guid>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32375.html</guid><wfw:comment>http://www.cnitblog.com/guopingleee/comments/32375.html</wfw:comment><comments>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/guopingleee/comments/commentRss/32375.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/guopingleee/services/trackbacks/32375.html</trackback:ping><description><![CDATA[
		<br />                                                                                                      <font size="5">   53. switch语句<br /></font><br /><br />/*--------------------------------------------------------------<br />         | 作者： Antigloss @ <a href="http://stdcpp.cn/"><a href="http://stdcpp.cn/" target="_blank"><font color="#333333">http://stdcpp.cn</font></a></a> @ 蚂蚁的 C/C++ 标准编程<br />         |  <br />         | 功能： 演示 switch 语句，统计一行字符中元音字母的个数<br />          -------------------------------------------------------------*/
<p>        #include <b>&lt;</b>stdio.h<b>&gt;</b><br />        #include <b>&lt;</b>ctype.h<b>&gt;</b></p><p>        int main(void)<br />        {<br />            int ch;  /* 注意，ch 一定要定义为 int 类型。参考<b><a href="http://cpp.ga-la.com/html/3/3/0704/242.htm" target="_blank"><font color="#333333">这里</font></a></b> */<br />            unsigned apple     = 0,<br />                     egg       = 0,<br />                     icecream  = 0,<br />                     orange    = 0,<br />                     umbrella  = 0,<br />                     other     = 0;</p><p>            printf("Please type in a sentence:\n");<br />            while ( (ch = getchar()) != '\n' )<br />            {<br />                switch ( tolower(ch) )  /* tolower 返回 ch 对应的小写字母的值 */<br />                {<br />                    case 'a' :<br />                        ++apple;<br />                        break;<br />                    case 'e' :<br />                        ++egg;<br />                        break;<br />                    case 'i' :<br />                        ++icecream;<br />                        break;<br />                    case 'o' :<br />                        ++orange;<br />                        break;<br />                    case 'u' :<br />                        ++umbrella;<br />                        break;<br />                    default :<br />                        ++other;<br />                        break;  /* 此行不写也没啥关系，习惯上还是写上的好 */<br />                }<br />            }</p><p>            printf("a    : %u\n"<br />                   "e    : %u\n"<br />                   "i    : %u\n"<br />                   "o    : %u\n"<br />                   "u    : %u\n"<br />                   "other: %u\n",<br />                   apple,<br />                   egg,<br />                   icecream,<br />                   orange,<br />                   umbrella,<br />                   other);</p><p>            return 0;<br />        }</p><p>    在往下看之前，请先想一下该程序是做什么用的，然后编译运行，看看结果和你想的是否一致。如果一致，那可以说你已经基本上学会了 switch 语句。以下是该程序运行的结果：</p><p>        Please type in a sentence:<br />        Welcome to <a href="http://stdcpp.cn/"><a href="http://stdcpp.cn/" target="_blank"><font color="#333333">http://stdcpp.cn</font></a></a>. Please enjoy yourself here.<br />        a    : 1<br />        e    : 8<br />        i    : 0<br />        o    : 4<br />        u    : 1<br />        other: 42</p><p>    下面我们讲解一下 switch 语句的用法。switch 语句的结构如下所示：</p><p>        switch (整型表达式)<br />        {<br />            case 整型常量1:<br />                一系列语句    // 可有可无<br />            case 整型常量2:<br />                一系列语句    // 可有可无<br />            case 整型常量3:<br />                一系列语句    // 可有可无<br />            // ... ...<br />            default :         // 可有可无<br />                一系列语句    // 可有可无</p><p>        }</p><p>其中，“整型表达式”指的是其值为整数类型（包括 char）的表达式；case 标签中的“整型常量”必须是整数类型（当然也包括 char）的常量或者常量表达式（只包含整型常量的表达式）。case 标签中不能使用变量！C99 标准规定，一个 switch 里至少可以用 1023 个 case 标签，而且不包括 switch 语句中的嵌套 switch 语句的 case 标签。原文如下：<br /><br /><br /></p><center><table cellspacing="0" cellpadding="0" width="90%" border="0"><tbody><tr><td><b> 引用</b>:<br /><br /></td></tr><tr><td><table cellspacing="1" cellpadding="10" width="100%" bgcolor="#cccccc" border="0"><tbody><tr><td style="WORD-BREAK: break-all" width="100%" bgcolor="#ffffff"><b>C99 - 5.2.4.1 Translation limits</b><br />1023 case labels for a switch statement (excluding those for any nested switch statements)<br /></td></tr></tbody></table></td></tr></tbody></table></center><br /><p></p><p>    下面，我们通过讲解上例的执行过程来学习 switch 语句。首先，getchar 读取用户输入的一个字符，然后该字符被赋值给 ch，接着判断 ch 是否为 '\n'（也就是判断是否已经读完一行），如果 ch 为 '\n'，则退出循环。以我们的输入为例：<br /><br /><br /></p><center><table cellspacing="0" cellpadding="0" width="90%" border="0"><tbody><tr><td><b> 引用</b>:<br /><br /></td></tr><tr><td><table cellspacing="1" cellpadding="10" width="100%" bgcolor="#cccccc" border="0"><tbody><tr><td style="WORD-BREAK: break-all" width="100%" bgcolor="#ffffff">Welcome to <a href="http://stdcpp.cn/"><a href="http://stdcpp.cn/" target="_blank">http://stdcpp.cn</a></a>. Please enjoy yourself here.</td></tr></tbody></table></td></tr></tbody></table></center><br /><br />while 循环第一次读到 'W'，然后进入循环体执行 switch 语句。switch 后面被括号括住的整型表达式会先被运算，本例中就是先调用 tolower 函数。tolower(ch) 的作用是返回 ch 对应的小写字母的值。如果 ch 不是字母或者本身就是小写字母，则返回的是 ch 本身的值。故此，此时表达式的值为 'w'。然后，程序开始扫描 case 标签（本例的 case 'a':、case 'e': 等等），直到在“整型常量”中找到一个和“整型表达式”的值匹配的值为止（本例是在 'a'、'e' 等等中找和 'w' 匹配的值）。当程序找到一个匹配的值后，就会执行该标签下的一系列语句。如果在 case 标签中找不到任何匹配的值，并且 switch 语句中有 default 标签，那么程序就会执行 default 标签下的一系列语句；否则，程序将退出 switch 语句，然后执行 switch 语句后面的语句。本例在 case 标签中找不到和 'w' 匹配的值，而且 switch 语句中有 default 标签，所以<br /><br /><br /><center><table cellspacing="0" cellpadding="0" width="90%" border="0"><tbody><tr><td><b> 引用</b>:<br /><br /></td></tr><tr><td><table cellspacing="1" cellpadding="10" width="100%" bgcolor="#cccccc" border="0"><tbody><tr><td style="WORD-BREAK: break-all" width="100%" bgcolor="#ffffff"><br />++other;<br />break;  /* 此行不写也没啥关系，习惯上还是写上的好 */<br /></td></tr></tbody></table></td></tr></tbody></table></center><br /><br />会被执行。 
<p></p><p>    你可能会觉得很奇怪——为什么每个标签后都要写上 break 语句呢，它在这里是干什么用的？其实，它在这里的作用是跳出 switch 语句，转而执行 switch 语句后面的语句。如下图所示（假设 number 的值为 2）：</p><p align="center"><img onmousewheel="return bbimg(this)" onmouseover="this.style.cursor='hand'" style="CURSOR: hand" onclick="{window.open('/csimage/switch.jpg');}" alt="" hspace="0" src="http://cpp.ga-la.com/csimage/switch.jpg" onload="if(this.width&gt;500) {this.resized=true; this.width=500;}" align="baseline" border="0" /></p><p>如果不写 break 语句，则从匹配的标签开始，直到 switch 语句结束前的语句都会被执行。例如，如果把上例中的 break 语句都去掉，重新编译后，在运行时输入 I，则会得到如下结果：</p><p>        Please type in a sentence:<br />        I<br />        a    : 0<br />        e    : 0<br />        i    : 1<br />        o    : 1<br />        u    : 1<br />        other: 1</p><p>由此可见，“case 'i' :”后面，switch 语句结束前的所有语句都被执行了一遍。因此，千万别忘了写上 break 语句，它非常重要！</p><p>    再回到我们前面的例子，while 循环第二次读到 'e'，和“case 'e' :”匹配，则执行<br /><br /><br /></p><center><table cellspacing="0" cellpadding="0" width="90%" border="0"><tbody><tr><td><b> 引用</b>:<br /><br /></td></tr><tr><td><table cellspacing="1" cellpadding="10" width="100%" bgcolor="#cccccc" border="0"><tbody><tr><td style="WORD-BREAK: break-all" width="100%" bgcolor="#ffffff"><br />++egg;<br />break;<br /></td></tr></tbody></table></td></tr></tbody></table></center><br /><br />while 循环第三次读到 'l'……（略） 
<p></p><p>    本例使用 tolower 函数（需包含头文件 ctype.h）是为了能统计大写的元音字母。当然，不用 tolower 也能达到统计大写元音字母的目的，只要为对应的大写字母添加一个标签即可。例如：</p><p>        switch ( ch )<br />        {<br />            case 'a' :<br />            case 'A' :<br />                ++apple;<br />                break;<br />            case 'e' :<br />            case 'E' :<br />                ++egg;<br />                break;<br />        ... ...</p><p>    break 语句既能用于跳出循环，也能用于跳出 switch，但 continue 语句只能作用于循环。当 switch 语句位于循环里时：<br /></p><ul><li>switch 语句中的 break 语句只跳出 switch 语句，而不会更进一步跳出包含该 switch 语句的循环。<br /></li><li>switch 语句中的 continue 语句将致使程序跳过循环中余下的语句（当然也包括 switch 语句中余下的语句），转而判断循环条件是否仍然成立，然后选择是否再次进入循环体。</li></ul><p></p><p>参考资料：C Primer 5th Edition<br />          C99 标准 - 6.8.4.2 The switch statement</p><img src ="http://www.cnitblog.com/guopingleee/aggbug/32375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/guopingleee/" target="_blank">向左向右走</a> 2007-08-24 11:18 <a href="http://www.cnitblog.com/guopingleee/archive/2007/08/24/32375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>2. 第一个 C 程序</title><link>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32371.html</link><dc:creator>向左向右走</dc:creator><author>向左向右走</author><pubDate>Fri, 24 Aug 2007 02:58:00 GMT</pubDate><guid>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32371.html</guid><wfw:comment>http://www.cnitblog.com/guopingleee/comments/32371.html</wfw:comment><comments>http://www.cnitblog.com/guopingleee/archive/2007/08/24/32371.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/guopingleee/comments/commentRss/32371.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/guopingleee/services/trackbacks/32371.html</trackback:ping><description><![CDATA[ <font size="3">首先请看下面这个简单的 C 程序，猜猜它会做些什么。猜不出来也没关系，后面会有详细的解说。<br /></font><p>         #include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h></stdio.h></stdio.h></stdio.h></p><p>         int main( void )            <font color="#4db361">/* 一个简单的程序 */<br /></font>         {<br />             int num;                  <font color="#55aa55">/* 声明一个名叫 num 的变量 */<br /></font>             num = 1;                 <font color="#55aa66"> /* 将 1 赋值给 num         */</font></p><p>             printf( "This is a simple C program.\n" ); <font color="#55aa66">/* 调用 printf 函数 */<br /></font>             printf( "My favorite number is %d because it is first.\n", num );</p><p>             return 0;<br />         }<br /><br />     <font size="3">在对这个程序进行详细说明之前，我想先请您用<strong>编译器</strong>把它<strong>编译</strong>成可执行文件，并且运行，看看运行结果和您的猜想是否一致。如果不懂如何进行编译，请参考以下文章：</font></p><p><span class="title"><strong><font color="#0000ff"><font size="3">    </font><a href="http://stdcpp.cn/html/1/15/0510/12.htm" target="_blank"><font color="#333333" size="3">Dev-C++安装&amp;使用教程</font></a><br /><font size="3">    </font><a href="http://stdcpp.cn/html/8/17/0511/93.htm"><font color="#333333" size="3">Dev-C++ 下载</font></a></font></strong></span></p><p>     <font size="3">下面，我将分两步对这个程序进行详细的讲解。第一步是<strong>概要说明</strong>，也就是对程序中每一行代码的含义作简单讲解，以帮助您对这个程序有一个大致的理解。第二步是<strong>详细说明</strong>，也就是程序中的每一行代码进行详细的讲解，以加深您的理解。</font></p><p> </p><p>    <font face="楷体_GB2312" size="5"><strong>一、概要说明</strong></font><br /></p><p>                 <font color="#e66b1a">#include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h>    <font color="#ff0000">&lt;-</font>   把另外一个文件包含进来</stdio.h></stdio.h></stdio.h></font><br />            <font size="2">这行代码命令编译器把<strong>头文件</strong>（<strong>.h</strong> 文件）<strong>stdio.h</strong> 中的内容<strong>包含</strong>到我们的<br />       程序中。stdio.h是C语言的<strong>标准头文件</strong>之一，提供对键盘输入和显示输出等的<br />       支持。要使用 <strong>printf</strong> 函数，就应该包含这个头文件。顺带一提，<strong>stdio</strong> 代表<br />       <strong>standard input/output</strong>，也就是<strong>标准输入输出</strong>的意思。</font></p><p>                 <font color="#e6941a">int main(void)    <font color="#ff0000">&lt;-</font>   </font><font color="#e6941a">函数头</font><font color="#000000"><br />             </font><font size="2">C 程序由一个或多个<strong>函数</strong>组成。我们的这个程序只有一个函数：<strong>main</strong> 函数。<br />       <strong>圆括号</strong><strong>()</strong> 表明 main 是一个函数；<strong>int</strong> 表明 main 函数返回一个整数；<strong>void</strong> 表明<br />       main 函数不接受任何参数。在此，请您紧记，int main(void) 是 <strong>ISO/ANSI C<br />     </strong> 标准中定义的 main 函数定义方法之一。</font></p><p>                 <font color="#d56f2b">/* a simple program */    <font color="#ff0000">&lt;-</font>   注释</font><br />             <font size="2"><font color="#0909f7">/*</font> 和 <font color="#1111ee">*/</font> 用于对程序进行<strong>注释</strong>说明，帮助读者更快更好地理解程序。编译器<br />       会忽略 /* 和 */ 之间的内容（包括 /* 和 */），不会对它们进行编译。</font></p><p>                 <font color="#dd6d22">{        <font color="#ff0000">&lt;-</font>   函数体的开端</font><br />             <font size="2">这个<strong>开大括弧 {</strong> 标志着<strong>函数体</strong>开始。函数体以<strong>闭大括弧 }</strong> 结束。在 { 和 }<br />       之间的<strong>语句</strong>都属于这个函数。</font></p><p>                 <font color="#dd9222">int num;     <font color="#ff0000">&lt;-</font>   声明变量 num<br /></font>             <font size="2">这个语句表明我们<strong>声明</strong>了一个名叫 num 的<strong>变量</strong>。int 表明这个变量为<br />       <strong>整型变量</strong>。</font></p><p>                 <font color="#dd6d22">num = 1;     <font color="#ff0000">&lt;-</font>   赋值语句<br /></font>             <font size="2">这个语句将 1 <strong>赋值</strong>给变量 num 。执行这个语句后，num 的值等于 1 。</font></p><p>                   <font color="#d5912b">printf( "This is a simple C program.\n" );    <font color="#ff0000">&lt;- </font>  函数调用语句</font><br /><font size="2">             <strong>printf</strong> 函数是 C 语言<strong>标准函数库里的函数</strong>。这个语句<strong>调用</strong> printf 函数在<br />        屏幕上显示“</font><font size="2">This is a simple C program.”，并且换行。<strong><font color="#2b6fd5">\n</font></strong> 是<strong>换行符</strong>，它提示<br />        printf 开始新的一行，也就是将<strong>光标</strong>移动到下一行的开端。如果上面的语句中没有<br />        \n ，那就不会换行，而且光标会紧跟在“This is a simple program.”后面。</font></p><p>                   <font color="#e6941a">printf( "My favorite number is %d because it is first.\n", num );</font><br /><font size="2">             <strong><font color="#338fcc">%d</font></strong> 告诉 printf 用num的值替换掉 %d ，并且以整数形式显示num的值。<br />        因为 num 的值为 1 ，所以这个语句在屏幕上显示“<strong>My favorite number is 1<br />       because it is first.</strong>”，并且换行。</font></p><p>                 <font color="#ee6911">return 0;    <font color="#ff0000">&lt;-</font>   返回语句</font><br />             <font size="2">C 程序应该<strong>返回</strong>一个数值给它的调用者。0 代表程序无误退出；而其它数字的含义<br />        则由系统决定。通常，非零（如 -1）代表程序运行中遇到某种错误而退出。</font></p><p>                 <font color="#ee6911">}     <font color="#ff0000">&lt;-</font>   函数体结束</font><br />          <font size="2">函数体以<strong>闭大括弧 }</strong> 结束。<br /></font></p><p><br />    <font face="楷体_GB2312" size="5"><strong>二、详细说明</strong></font><br /></p><font size="2"></font><p><font size="2">         现在，让我们进一步来了解每一行代码的含义。下面的内容很<strong>基础</strong>，也很<strong>重要</strong>，<br />     必须<strong>认真弄懂</strong>，牢牢<strong>掌握</strong>。</font></p><p>          <font size="3"><strong><font color="#e66b1a">1.</font></strong><font color="#0968f7">#include</font><strong>预处理指令</strong>和<strong>头文件</strong></font><br /></p><p>             <font size="2"> 在编译器对我们写的<strong>源程序</strong>进行编译之前，<strong>预处理器</strong>会对我们的写的</font><font size="2"><strong>源代码<br />       </strong>进行必要的处理，称为<strong>预处理</strong>。#include 就是一个<strong>预处理指令</strong>，它命令预处理器<br />        进行的预处理是：<strong>把特定的头文件包含到我们的源代码里</strong>。<br />            #include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h>的作用是将头文件 stdio.h 里的内容包含进我们的程序，<br />        产生的结果是将 stdio.h 里的内容一字不漏地插入 #include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h>出现的<br />        地方，并且删除 #include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h>。换句话说，就是用 stdio.h 里的内容替换<br />        #include <strong>&lt;</strong>stdio.h<strong>&gt;</strong><stdio.h><stdio.h><stdio.h>。这个过程是在预处理阶段完成的。C 语言的这种包含文件的<br />        机制为多个程序<strong>共享</strong>相同信息提供了极大的方便。<br />            stdio.h 是 C 语言标准中定义了的头文件，它包含着<strong>输入输出函数</strong>的信息。<br />        所有 C 编译器都应该有这个头文件，这是 <strong>ANSI/ISO C</strong> 标准中规定的。当然，<br />        ANSI/ISO C 规定的编译器必须提供的头文件可不止这么一个。标准还规定了许多<br />        别的头文件，那些我们会在以后慢慢学到。<br />            有些程序需要包含 stdio.h ，而有些不需要。如果上面这个例子中没有使用<br />        printf 函数，那就不需要包含 stdio.h 。<br />            头文件中包含着编译器进行编译时所需的信息。头文件中可能指明了函数名和<br />        函数调用方法，但是这些函数的<strong>实现代码</strong>并不在头文件中，而是在<strong>预先编译</strong>好了的<br />        <strong>库文件</strong>里。<br />            <strong>链接器</strong>负责在库文件中寻找我们的程序所需的代码，并且把那些代码和我们写的<br />        程序进行<strong>链接</strong>，从而将我的写的程序链接成<strong>可执行文件</strong>。一句话，头文件用于指导<br />        编译器正确地将我们写的源程序编译成可执行文件。</stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></stdio.h></font></p><p>          <font size="3"><strong><font color="#ee6911">2.</font></strong><font color="#1a94e6">main</font> 函数<br /></font></p><p>               <font size="2">在 C 语言中，main 函数是<strong>主函数</strong>。每个 C 程序都<strong>必须有且只能有一个<br />       </strong>main 函数（主函数）！<font color="#ff0000">C 程序从 main 函数开始执行，并且在 main 函数中结束</font>。<br />            int是 main 函数的<strong>返回值类型</strong>，它表明 main 函数应该返回一个整数给程序的<br />        调用者（如操作系统）。<br />            如果您想了解更多关于 main 函数的东西，请点击下面的链接：<br /></font>              <a href="http://cpp.ga-la.com/html/3/3/0510/5.htm"><span class="title"><strong><font color="#0000ff" size="3">C/C++ 误区一：void main</font></strong></span></a></p><p>          <font size="3"><strong><font color="#ee6911">3.</font></strong> 注释</font><br /></p><p>                    <font color="#1111ee">/* a simple program */</font><br />              /* */ 之间的内容都是<strong>注释</strong>。对程序进行适当的注释可以帮助读者（包括您自己）<br />          更快更好地理解您写的代码。注释可以在源程序中的任何地方书写。/* 和 */ 之间的<br />          任何内容（包括 /* 和 */）都会被编译器忽略掉。注意：/ 和 * 必须紧挨在一起，<br />          它们之间不能有空格。<br />                     <font color="#0909f7">/* 我是注释 */<br />                     /*我是<br />                       跨行注释*/<br />                     /*<br />                       我也是注释<br />                     */<br /></font>              以上的注释都是合法的；但下面的注释是非法的，因为没写<strong>注释结束标记</strong> */。<br />                     <font color="#ff0000">/* 非法，没有注释结束标记<br /></font>              下面这个注释可能会出乎你的意料：<br />                     <font color="#3809f7">/* 我是<strong>注释开始标记</strong><br />                     /* 哦哦，我不是注释开始标记<br />                        我是注释结束标记，我<strong>和第一个 /* 匹配</strong>，而不是和第二个 /* 匹配。*/<br /></font>              上面这个注释中，<strong><font color="#ff0000">注释结束标记 <font color="#0909f7">*/</font> 和第一个 <font color="#1a1ae6">/*</font> 匹配</font></strong>，它们之间的内容<br />          都被视为注释。</p><p>              <strong>C99 </strong>中增加了另外一种注释方式，这种注释方式使用 <font color="#3d11ee">//</font> 作为<strong>注释符</strong>，和 <font color="#1111ee">//<br /></font>          处于同一行，并且在 <font color="#1a42e6">//</font> 右边的内容都被当作注释。<font color="#3d11ee">/</font> 和<font color="#1a1ae6"> /</font> 之间不能有空格。<br />                     <font color="#1111ee">// 我是注释</font><br />                     int rigue;      <font color="#0909f7">//注释放在这里也是合法的</font><br />              上面的注释在 C99 中是合法的，但是在 <strong>C89</strong> 中是非法的。也就是说，老旧的<br />          编译器可能不支持 <font color="#0938f7">//</font> 注释符。<strong>VC6</strong> 支持 <font color="#1111ee">//</font>，<strong>TC2</strong> 不支持。<br />               <br />          <font size="3"><strong><font color="#e66b1a">4.</font></strong> 大括号与函数体</font><br /></p><p>               <font size="2">C 语言中，所有函数都以大括弧（<font color="#1196ee"><strong>{ }</strong></font>）作为<strong>界定符</strong>。函数体从开大括弧 {<br />        开始，以闭大括弧 } 结束。在后续教程中，我会对函数进行更详细的讲解。</font></p><p>          <font size="3"><strong><font color="#e66b1a">5.</font></strong> int num;</font><br /></p><p>               <font size="2">这个语句<strong>声明</strong>了<strong>变量</strong> num 。这一行末尾的<strong>分号</strong>（<font color="#ff0000">;</font>）表明这一行<br />        是一个<strong>语句</strong>。<font color="#ff0000">;</font> 是语句的一部分，单独一个 <font color="#ff0000">; </font>也是一个合法的 C 语句。<br />             <font color="#0909f7">int</font> 是 C 语言中的一个<strong>关键字</strong>，它代表 C 语言里的一种基本数据类型：整型。<br />        关键字是语言的<strong>保留字</strong>，不能用作其它用途。例如：我们不能关键字来做变量名或者<br />        函数名。<br />             <strong>标识符</strong>是指我们为变量或函数等取的名字。在这个语句中，num 就是标识符。<br />             C 语言中，所有变量在使用前都必须声明。C89 中，变量的声明必须位于<br />        <strong>函数体</strong>或者<strong>程序块</strong>的开端。例如：<br />                  int main( void ) <font color="#1111ee">/* C89 */</font><br />                  { <font color="#1111ee">/* 函数体开始 */</font><br />                      int doors;<br />                      int dogs;<br />                      doors = 5;<br />                      dogs = 3;<br />                      { <font color="#0909f7">/* 程序块开始 */</font><br />                           int dig;<br />                           int dug;<br />                           dig = 6;<br />                           dug = 8;<br />                      } <font color="#3d11ee">/* 程序块结束 */</font></font></p><p><font size="2">                      return 0;<br />                  } <font color="#3809f7">/* 函数体结束 */</font><br />             而在 C99 中，声明可以位于函数体或者程序块的任何地方。但是，变量仍然<br />        需要在声明之后才可使用。例如：<br />                  int main( void )<font color="#3809f7"> /* C99 */<br /></font>                  { <font color="#3809f7">/* 函数体开始 */</font><br />                      int doors;<br />                      doors = 5;<br /></font><font size="2">                      { <font color="#3809f7">/* 程序块开始 */</font><br />                          int dig;<br />                          dig = 6;<br />                          int dug;<br />                          dug = 8;<br />                      } <font color="#1111ee">/* 程序块结束 */<br /></font></font><font size="2">                      int dogs;<br />                      dogs = 3;<br /><br /></font><font size="2">                      return 0;<br />                  } <font color="#3809f7">/* 函数体结束 */</font><br />             上面的程序在 C99 中是合法的，但是在 C89 中却是非法的。当前支持C99 的<br />        编译器不多，所以上面的代码您的编译器很有可能编译不了。故而，为了使我们的<br />        代码具有良好的<strong>可移植性</strong>，我们应该尽可能少用，甚至不用 C99 中的新特性。</font></p><p><font size="2">        <font size="3"><strong><font color="#dd6d22">6.</font></strong> 赋值</font><br /></font></p><p><font size="2">                  <font color="#dd6d22">num = 1;</font><br />             <strong>赋值</strong>是 C 语言中的一种基本操作。上面的语句是一个<strong>赋值语句</strong>，它的含义<br />        是：将 1 赋值给变量 num 。赋值操作完成后，num 的值等于 1 。</font></p><p><font size="2">        <font size="3"><strong><font color="#e6941a">7.</font></strong><font color="#1a42e6">printf</font> 函数</font><br /></font></p><p><font size="2">             <font color="#dd6d22">printf( "This is a simple C program.\n" );<br />             printf( "My favorite number is %d because it is first.\n", num );</font><br />            这两行代码都调用了 printf 函数。printf 是一个<strong>标准 C 函数</strong>，也就是说，<br />        它是 C 语言<strong>标准函数库</strong>里定义了的函数。圆括号 () 表明 printf 是一个函数，<br />        圆括号里的内容会被<strong>传递</strong>给 printf 函数。例如，上面第一行代码把“This is a<br />        simple C program.\n”（不包括双引号）传递给 printf 函数，然后由 printf<br />        函数把这些信息显示于显示器。<strong>\n</strong> 是<strong>换行符</strong>，它提示 printf 开始新的一行。<br />        正因为 \n 是换行符，所以 \n 不会显示出来，而是进行了换行。如果上面的语句中<br />        没有 \n ，那就不会换行。<br />            上面代码第二行中的 <strong>%d</strong> 告诉 printf 用 num 的值替换掉 %d ，并且以<br />        <strong>整数形式</strong>显示 num 的值。<br />             %d 被称为<strong>占位符</strong>，它告诉 printf 应该在什么地方显示 num 的值。% 用于<br />        提醒 printf 在此处显示一个变量的值（本例中是 num 的值）；d 告诉 printf 以<br />        <strong>十进制</strong>整数的形式显示 num 的值。</font></p><p><font size="2">        <font size="3"><strong><font color="#e66b1a">8.</font><font color="#3809f7">return</font>（返回）语句</strong></font><br /></font></p><p><font size="2">                   <font color="#dd6d22">return 0;<br /></font>             这个 <strong>return 语句</strong>是我们程序中的最后一个语句。int main( void ) 中的 int<br />        表示 main 函数应该返回一个整数，所以我们要使用 return 语句来返回一个整数。<br />             如果您想了解更多关于<strong>返回值</strong>的东西，请点击下面链接，查看文章的<strong>第四点</strong>。<br /></font></p><p>                   <a href="http://stdcpp.cn/html/3/3/0510/5.htm"><span class="title"><strong><font color="#0000ff" size="3">C/C++ 误区一：void main</font></strong></span></a></p><p>           <br /><font size="3">    上面所说的某些内容您<strong>暂时可能还不太理解</strong>，但是<strong>不用害怕</strong>，牢牢地<strong>把它们记住</strong>，然后<strong>继续往下学</strong>吧！随着学习的深入，就会<strong>逐渐搞明白</strong>。现阶段只要有这个概念就足够了。 </font></p><p><font size="3"><strong>参考资料：</strong>C Primer Plus, 5th Edtion By Stephen Prata</font></p><!----><p><font color="#ff6600">本文版权归 <a href="http://stdcpp.cn/"><strong><font color="#333333">蚂蚁的 C/C++ 标准编程</font></strong></a> 以及 作者 <font color="#ff0000">antigloss</font> 共同所有，转载请注明原作者和出处。谢谢。</font></p><!----><!-- google ads. --><script type="text/javascript"><!--
			google_ad_client = "pub-5913381155274424";
			google_ad_width = 468;
			google_ad_height = 60;
			google_ad_format = "468x60_as";
			google_ad_type = "text_image";
			google_ad_channel ="";
			google_color_border = "FFFFFF";
			google_color_link = "006666";
			google_color_bg = "FFFFFF";
			google_color_text = "000000";
			google_color_url = "959595";
			//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><!-- google ads. --><br /><script type="text/javascript"><!--
		google_ad_client = "pub-5913381155274424";
		google_ad_width = 468;
		google_ad_height = 60;
		google_ad_format = "468x60_as_rimg";
		google_cpa_choice = "CAAQjMeU_AEaCCfCybguyZX1KLj39IMB";
		//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><img src ="http://www.cnitblog.com/guopingleee/aggbug/32371.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/guopingleee/" target="_blank">向左向右走</a> 2007-08-24 10:58 <a href="http://www.cnitblog.com/guopingleee/archive/2007/08/24/32371.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>