在本教程中,我們將學(xué)習(xí)關(guān)鍵字(C ++編程中的保留關(guān)鍵字,它們是語(yǔ)法的一部分)。另外,我們還將學(xué)習(xí)標(biāo)識(shí)符以及如何命名它們。
關(guān)鍵字是預(yù)定義的單詞,對(duì)編譯器具有特殊的含義。例如,
int money;
這里,int是一個(gè)關(guān)鍵字,表示money是整數(shù)類(lèi)型的變量。
這是所有C ++關(guān)鍵字的列表。(從C ++ 17開(kāi)始)
| alignas | decltype | namespace | struct |
| alignof | default | new | switch |
| and | delete | noexcept | template |
| and_eq | do | not | this |
| asm | double | not_eq | thread_local |
| auto | dynamic_cast | nullptr | throw |
| bitand | else | operator | true |
| bitor | enum | or | try |
| bool | explicit | or_eq | typedef |
| break | export | private | typeid |
| case | extern | protected | typename |
| catch | false | public | union |
| char | float | register | unsigned |
| char16_t | for | reinterpret_cast | using |
| char32_t | friend | return | virtual |
| class | goto | short | void |
| compl | if | signed | volatile |
| const | inline | sizeof | wchar_t |
| constexpr | int | static | while |
| const_cast | long | static_assert | xor |
| continue | mutable | static_cast | xor_eq |
注意:由于C ++是區(qū)分大小寫(xiě)的語(yǔ)言,因此所有關(guān)鍵字都必須用小寫(xiě)字母編寫(xiě)。
標(biāo)識(shí)符是程序員給變量、類(lèi)、函數(shù)或其他實(shí)體的唯一名稱(chēng)。例如,
int money; double accountBalance;
在這里,money和accountBalance是標(biāo)識(shí)符。
標(biāo)識(shí)符可以由字母,數(shù)字和下劃線(xiàn)字符組成。
它對(duì)名稱(chēng)長(zhǎng)度沒(méi)有限制。
它必須以字母或下劃線(xiàn)開(kāi)頭。
區(qū)分大小寫(xiě)。
我們不能將關(guān)鍵字用作標(biāo)識(shí)符。
如果遵循上述規(guī)則,我們可以選擇任何名稱(chēng)作為標(biāo)識(shí)符。但是,我們應(yīng)該為有意義的標(biāo)識(shí)符提供有意義的名稱(chēng)。
| 不合法的識(shí)別符 | 錯(cuò)誤的標(biāo)識(shí)符 | 好的標(biāo)識(shí)符 |
|---|---|---|
| Total points | T_points | totalPoint |
| 1list | list_1 | list1 |
| float | n_float | floatNumber |