<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                本文目錄 - [一、枚舉的概念](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#label0) - [二、枚舉類型的定義](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#label1) - [三、枚舉變量的定義](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#label2) - [四、枚舉使用的注意](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#label3) - [五、枚舉變量的基本操作](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#label4) 說明:這個C語言專題,是學習iOS開發的前奏。也為了讓有面向對象語言開發經驗的程序員,能夠快速上手C語言。如果你還沒有編程經驗,或者對C語言、iOS開發不感興趣,請忽略 上一講介紹了[結構體](http://www.cnblogs.com/mjios/archive/2013/03/24/2977910.html)類型,這講就介紹C語言中的另一種數據類型---枚舉類型。枚舉類型在iOS中也是很常用的,用法跟Java中的枚舉類似。 [回到頂部](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#labelTop) ### 一、枚舉的概念 枚舉是C語言中的一種基本數據類型,并不是構造類型,它可以用于聲明一組常數。當一個變量有幾個固定的可能取值時,可以將這個變量定義為枚舉類型。比如,你可以用一個枚舉類型的變量來表示季節,因為季節只有4種可能的取值:春天、夏天、秋天、冬天。 [回到頂部](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#labelTop) ### 二、枚舉類型的定義 一般形式為:enum 枚舉名 {枚舉元素1,枚舉元素2,……}; ~~~ enum Season {spring, summer, autumn, winter}; ~~~ [回到頂部](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#labelTop) ### 三、枚舉變量的定義 前面只是定義了枚舉類型,接下來就可以利用定義好的枚舉類型定義變量。 跟結構體一樣,有3種方式定義枚舉變量 ### 1.先定義枚舉類型,再定義枚舉變量 ~~~ enum Season {spring, summer, autumn, winter}; enum Season s; ~~~ ### 2.定義枚舉類型的同時定義枚舉變量 ~~~ enum Season {spring, summer, autumn, winter} s; ~~~ ### 3.省略枚舉名稱,直接定義枚舉變量 ~~~ enum {spring, summer, autumn, winter} s; ~~~ 上面三種方式定義的都是枚舉變量s [回到頂部](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#labelTop) ### 四、枚舉使用的注意 1> C語言編譯器會將枚舉元素(spring、summer等)作為整型常量處理,稱為枚舉常量。 2> 枚舉元素的值取決于定義時各枚舉元素排列的先后順序。默認情況下,第一個枚舉元素的值為0,第二個為1,依次順序加1。 ~~~ enum Season {spring, summer, autumn, winter}; ~~~ 也就是說spring的值為0,summer的值為1,autumn的值為2,winter的值為3 3>?也可以在定義枚舉類型時改變枚舉元素的值 ~~~ enum season {spring, summer=3, autumn, winter}; ~~~ 沒有指定值的枚舉元素,其值為前一元素加1。也就說spring的值為0,summer的值為3,autumn的值為4,winter的值為5 [回到頂部](http://www.cnblogs.com/mjios/archive/2013/03/24/2979390.html#labelTop) ### 五、枚舉變量的基本操作 ### 1.賦值 可以給枚舉變量賦枚舉常量或者整型值 ~~~ enum Season {spring, summer, autumn, winter} s; s = spring; // 等價于 s = 0; s = 3; // 等價于 s = winter; ~~~ ### 2.遍歷枚舉元素 ~~~ enum Season {spring, summer, autumn, winter} s; // 遍歷枚舉元素 for (s = spring; s <= winter; s++) { printf("枚舉元素:%d \n", s); } ~~~ 輸出結果:![](https://box.kancloud.cn/2016-05-07_572d767aba335.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看