📄 enum.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>enum</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C/C++ Keywords</a> > <a href="enum.html">enum</a> </div> <div class="name-format"> enum </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> enum name {name-list} var-list;</pre> <p>The enum keyword is used to create an enumerated type named name that consists of the elements in <em>name-list</em>. The <em>var-list</em> argument is optional, and can be used to create instances of the type along with the declaration. For example, the following code creates an enumerated type for colors:</p> <div class="related-examples"> <pre class="example-code"> enum ColorT {red, orange, yellow, green, blue, indigo, violet}; ... ColorT c1 = indigo; if( c1 == indigo ) { cout << "c1 is indigo" << endl; } </pre></div> <p>In the above example, the effect of the enumeration is to introduce several new constants named <em>red</em>, <em>orange</em>, <em>yellow</em>, etc. By default, these constants are assigned consecutive integer values starting at zero. You can change the values of those constants, as shown by the next example:</p> <div class="related-examples"> <pre class="example-code"> enum ColorT { red = 10, blue = 15, green }; ... ColorT c = green; cout << "c is " << c << endl;</pre></div> <p>When executed, the above code will display the following output:</p> <div class="related-examples"> <pre class="example-code"> c is 16</pre></div> <p>Note that the above examples will only work with C++ compilers. If you're working in regular C, you will need to specify the <em>enum</em> keyword whenever you create an instance of an enumerated type: <div class="related-examples"> <pre class="example-code"> enum ColorT { red = 10, blue = 15, green }; ... enum ColorT c = green; // note the aditional enum keyword printf( "c is %d\n", c );</pre></div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -