1.關(guān)于函數(shù)定義時(shí)的省略情形
在函數(shù)定義時(shí)抹腿,如果省略函數(shù)前面的返回值類型:在C標(biāo)準(zhǔn)中默認(rèn)返回int值尸闸,但在C++語(yǔ)言中,并沒(méi)有該規(guī)定韵吨,其必須有返回值類型迹冤。
在函數(shù)定義時(shí)讽营,如果省略參數(shù)列表:在C++中,默認(rèn)該函數(shù)無(wú)參數(shù)(void)泡徙,但在C語(yǔ)言中橱鹏,并無(wú)該規(guī)定,必須明確表示有無(wú)參數(shù)列表堪藐;
main函數(shù)必須有返回值莉兰,所以即使你定義了main函數(shù)的返回值類型為void,編譯器同樣會(huì)在程序結(jié)束前隱式添加
return 0礁竞;
語(yǔ)句糖荒。所以,在函數(shù)定義時(shí)模捂,為了減少錯(cuò)誤的發(fā)生捶朵,無(wú)論C或是C++,我們都要明確所定義函數(shù)的返回值類型和參數(shù)列表狂男。
2.關(guān)于注釋符
C風(fēng)格的注釋符為
/* */
,它可以位于單獨(dú)行或跨行综看,但起始和終止符必須配對(duì)使用C++風(fēng)格的注釋符為
//
,它僅位于單獨(dú)行(也可位于代碼行后面)岖食,以//
為起始红碑,行尾為終止符。C++兼容C的注釋符泡垃,C99以后版本也兼容C++的注釋符
3.關(guān)于文件擴(kuò)展名
- C++源文件的擴(kuò)展名有:
.cpp
析珊,.C
,.cxx
兔毙,取決于你的C++系統(tǒng)唾琼。
4. 關(guān)于頭文件
- 在使用C++的輸入輸出函數(shù)(cout和cin)時(shí)兄春,在編輯代碼前必須包含兩句:
#include <iostream.h>
適用于舊版本C++編譯器
或最新版本編譯器
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;</pre>
關(guān)于頭文件的使用澎剥,C++舊版本是和C一樣的,使用.h
作為擴(kuò)展名(例如:#include <iostream.h>),但后來(lái)為了區(qū)分是C還是C++的頭文件哑姚,新版C++規(guī)定頭文件不再添加.h
的擴(kuò)展(例如:#include <iostream>)祭饭,但有些共同的頭文件,C++又以在原文件名前添加c
的形式表示叙量,例如C中的math.h
倡蝙,在C++以cmath
表示。
5. 關(guān)于命名空間
- 在使用C++的輸入輸出函數(shù)(cout和cin)時(shí)绞佩,在編輯代碼前必須包含兩句:
#include <iostream.h>
(適用于舊版本C++編譯器)
或最新版本編譯器:(所有函數(shù)均默認(rèn)使用std命名空間)
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;//其作用范圍取決于它的位置
...
cout<<"hello!"<<endl;</pre>
或者僅將需要用到的函數(shù)在編碼前明確使用std命名空間)
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using std::cout; //或在使用時(shí)直接全路徑調(diào)用
using std::cin;
using std::endl;
...
cout<<"hello!"<<endl;</pre>
6. 關(guān)于變量聲明和賦值語(yǔ)句
- 在早期的C語(yǔ)言中寺鸥,為了方便管理,一般將一段程序中的所有變量聲明集中放在程序開(kāi)頭位置處品山,但后來(lái)的C++及C99以后胆建,規(guī)定變量聲明應(yīng)該緊挨著變量第一次使用前的位置.
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> ...
int var;
var = 10;
...</pre>
- 賦值操作符
=
可以串聯(lián)使用
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n50" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int Var1,Var2,Var3;
Var1 = Var2 = Var3 = 10;</pre>
- 賦值操作符
=
可以自引用
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Var = Var - 1;</pre>
7. cout和printf()
- 在C語(yǔ)言中,利用printf()函數(shù)輸出一個(gè)數(shù)字時(shí)肘交,需要用%d明確告知笆载,但cout會(huì)自動(dòng)判別并自動(dòng)輸出
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n59" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> printf("Printing a string: %s", "25");
printf("Printing a string: %d", 25);</pre>
C++中:
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n61" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int Var = 25;
cout << Var;</pre>
此處,之所以cout可以識(shí)別Var,是因?yàn)檎巫兞繉?duì)<<
操作符進(jìn)行了符號(hào)重載,其完成了2件事:一是取得變量Var的二進(jìn)制數(shù)值大小 ;二是將該數(shù)值轉(zhuǎn)化成相應(yīng)的數(shù)字字符串并輸出.cin同理.
- 另外通過(guò)
hex,oct
關(guān)鍵字涯呻,可以改變cout輸出數(shù)字的進(jìn)制(默認(rèn)輸出10進(jìn)制dec)凉驻。例如:
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << “Monsieur cuts a striking figure!” << endl;
cout << “chest = “ << chest << “ (decimal)” << endl;
cout << hex; // manipulator for changing number base
cout << “waist = “ << waist << “ hexadecimal” << endl;
cout << oct; // manipulator for changing number base
cout << “inseam = “ << inseam << “ (octal)” << endl;
return 0;
}
輸出:
Monsieur cuts a striking figure!
chest = 42 (decimal)
waist = 2a hexadecimal
inseam = 52 (octal)</pre>
However, if you omitted the using directive and instead used std::cout, std::endl, std::hex, and std::oct, you could still use plain hex as the name for a variable.
8. 關(guān)于變量名
C++中:
變量名僅能由字母、數(shù)字和下劃線組成复罐,且數(shù)字不能開(kāi)頭涝登。
以雙下劃線開(kāi)頭或單下劃線加大寫(xiě)字母開(kāi)頭的變量是系統(tǒng)保留的,用戶不得使用.
以單下劃線開(kāi)頭命名的變量也是系統(tǒng)保留的,一般視為全局變量市栗,用戶不得使用.
在C語(yǔ)言中缀拭,變量名只有前63個(gè)字符是有意義的。也就是說(shuō)兩個(gè)變量名填帽,如果他們的前63個(gè)字符是相同的蛛淋,則C編譯器認(rèn)為該兩個(gè)變量就是同一個(gè)。而在C++中篡腌,變量名長(zhǎng)度沒(méi)有限制褐荷,所有字符均有意義。
9.關(guān)于整形
在任一系統(tǒng)中嘹悼,short\int\long必須滿足以下條件
short至少16位寬
int位寬不得小于short
long至少32位叛甫,且不得小于int
long long至少64位,且不得小于long
整型變量類型的選擇:
int是最常選擇的杨伙,因?yàn)橛?jì)算機(jī)對(duì)它的處理效率最高
如果你能確定變量的值不會(huì)取負(fù)其监,那就選擇unsigned類型的。
如果變量的值比較大(大于16位數(shù)所能表示)限匣,那就選long型
如果需要考慮到內(nèi)存資源消耗抖苦,特別是需要定義一個(gè)很大的整型數(shù)組時(shí),那就選short
如果你僅僅使用單字節(jié),那就選char锌历。
來(lái)自limits.h文件的符號(hào)常量:
符號(hào)常量 | 含義 |
---|---|
CHAR_BIT | char類型的位數(shù) |
CHAR_MAX | Maximum char value |
CHAR_MIN | Minimum char value |
SCHAR_MAX | Maximum signed char value |
SCHAR_MIN | Minimum signed char value |
UCHAR -MAX | Maximum unsigned char value |
SHRT_MAX | Maximum short value |
SHRT_MIN | Minimum short value |
USHRT_MAX | Maximum unsigned short value |
INT_MAX | Maximum int value |
INT_MIN | Minimum int value |
UINT_MAX | Maximum unsigned int value |
LONG_MAX | Maximum long value |
LONG_MIN | Minimum long value |
ULONG_MAX | Maximum unsigned long value |
9.1 關(guān)于賦值
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int owls = 101; // traditional C initialization
int wrens(432); // alternative C++ syntax, set wrens to 432</pre>
9.2 關(guān)于自定義符號(hào)常量
在C語(yǔ)言中贮庞,我們一般會(huì)利用#define
語(yǔ)句來(lái)定義自己的符號(hào)常量,但在C++中究西,我們一般會(huì)使用const
關(guān)鍵字來(lái)定義一個(gè)符號(hào)常量(名字一般采用全大寫(xiě)字母)窗慎,并在定義的同時(shí)進(jìn)行賦值。因?yàn)楹笳呖梢灾付ǔW兞康臄?shù)據(jù)類型卤材,可以控制作用范圍遮斥,可以修飾更多復(fù)雜數(shù)據(jù)類型(數(shù)組、結(jié)構(gòu)體)扇丛,所以比define宏更具優(yōu)勢(shì)伏伐。
當(dāng)然在C語(yǔ)言中也可以使用const關(guān)鍵字,但其與C++中的最主要的區(qū)別:一是在于作用范圍晕拆;二是在C++中(不在C中)藐翎,可以使用const值來(lái)定義一個(gè)數(shù)組的大小。
2.0版C++之前实幕,和C語(yǔ)言一樣吝镣,用int型來(lái)存儲(chǔ)字符常量的值(但字符變量仍用char型存儲(chǔ)),但在之后C++就用char型來(lái)存儲(chǔ)字符常量的值了昆庇。
字符常量的表示方法:
單引號(hào)加字符:例如 ‘a(chǎn)’
反斜杠加字母表示特殊字符:
\a
(響鈴)末贾,\n
(換行),\r
(回車),\b
(回退)反斜杠加八進(jìn)制或十六進(jìn)制數(shù)字:
\032
或\x1a
表示CTRL+Z\u加8個(gè)16進(jìn)制數(shù) 或 \U加16個(gè)16進(jìn)制數(shù)(這些數(shù)字代表ISO 10646字符集編碼):\u00E2 代表 a
9.3 關(guān)于數(shù)字常量的默認(rèn)適配類型
對(duì)于單純的整型數(shù)字來(lái)說(shuō),如果不加任何修飾整吆,則一般認(rèn)為其為十進(jìn)制整數(shù)拱撵,如2000;
-
如果在數(shù)字后添加
l
或L
則代表此數(shù)字為long型表蝙,添加u
或U
則代表unsigned- 因?yàn)樾?xiě)的l容易與1混淆拴测,一般使用
L
,UL
或LU
都代表unsigned long意思
- 因?yàn)樾?xiě)的l容易與1混淆拴测,一般使用
-
在C++中府蛇,默認(rèn)會(huì)選擇最小適配的整數(shù)類型存儲(chǔ)給定的整型常量
對(duì)于十進(jìn)制整數(shù):依次匹配
int
集索、long
、unsigned long
汇跨;對(duì)于八進(jìn)制或十六進(jìn)制:依次匹配
int
务荆、unsigned int
,long
穷遂、unsigned long
函匕;
9.4 關(guān)于char
不同關(guān)于int,long型蚪黑,char既不明確代表signed char盅惜,也不明確代表 unsigned char吸耿,不同的系統(tǒng)選擇也不同,所以為了程序的兼容性酷窥,開(kāi)發(fā)者可以自己明確。
因?yàn)閏har只有8位伴网,最多僅表示256個(gè)不同的字符蓬推,在系統(tǒng)使用的字符集超過(guò)這個(gè)數(shù)后,char型就不足以表示全部的字符了澡腾。為了解決該問(wèn)題沸伏,C++使用wchar_t
類型來(lái)表示擴(kuò)展的字符集,它本質(zhì)上是16位二進(jìn)制整數(shù)动分,既可以是unsigned short毅糟,也可以是int類型。
C++采用在字符或字符串前綴 ‘L’ 表示寬字符澜公,例如:L’P’ 或 L”tall”
C++使用wcout 和 wcin來(lái)處理wchar_t類型的字符流
10. 關(guān)于浮點(diǎn)型
C++中有3種浮點(diǎn)型數(shù)據(jù):float(32位)姆另、double(64位)、long double(80-128位)
在C語(yǔ)言中的float.h或C++中的cfloat頭文件坟乾,規(guī)定了實(shí)數(shù)有效數(shù)字和尾數(shù)等的值:
- 在C++中有兩種表示方法:一是固定小數(shù)點(diǎn)的方式1212.3456迹辐,二是使用E或e的方式12.123456e2;使用cout命令輸出實(shí)數(shù)時(shí)需要設(shè)置輸出方式甚侣,為此:
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> cout.setf(ios_base::fixed, ios_base::floatfield) //fixed-point
cout.setf(ios::fixed, ios::floatfield) //fixed-point C++老版本 </pre>
10.1 關(guān)于浮點(diǎn)常數(shù)
默認(rèn)浮點(diǎn)常數(shù)采用double型存儲(chǔ),若在常量后綴f或F殷费,則采用float型存儲(chǔ)印荔,若在常量后綴l或L則用long double型存儲(chǔ)。因?yàn)樾?xiě)的l很像數(shù)字1详羡,所以一般使用L仍律。
float最大只能保證6位有效數(shù)字的精度,超過(guò)6位有效數(shù)字時(shí)实柠,建議使用duoble型
10.2 關(guān)于浮點(diǎn)數(shù)的運(yùn)算
除法(/):若2個(gè)操作數(shù)均為整數(shù)染苛,則結(jié)果為整數(shù);若至少有一個(gè)操作數(shù)為浮點(diǎn)數(shù)主到,則結(jié)果為浮點(diǎn)數(shù)茶行。
取余(%):左右操作數(shù)必須為整數(shù),否則報(bào)錯(cuò)登钥!
10.3 關(guān)于類型轉(zhuǎn)換
類型轉(zhuǎn)換發(fā)生在賦值畔师、運(yùn)算表達(dá)式和參數(shù)傳遞時(shí):
賦值時(shí)的轉(zhuǎn)換:整數(shù)向浮點(diǎn)數(shù)轉(zhuǎn)換時(shí)后面添加小數(shù)0;浮點(diǎn)數(shù)向整數(shù)轉(zhuǎn)換時(shí)截?cái)嗪竺娴男?shù)牧牢,但若超過(guò)整數(shù)范圍時(shí)無(wú)確定意義看锉;
表達(dá)式中的轉(zhuǎn)換:C++將自動(dòng)將
bool,char,unsigned char,signed char,short姿锭,unsigned short
的值轉(zhuǎn)化為int
型值再做計(jì)算(true一般轉(zhuǎn)為1,false轉(zhuǎn)化為0)伯铣。若short 和int的位數(shù)一樣呻此,則unsigned short轉(zhuǎn)化為unsigned int。類似的wchar_t類型將自動(dòng)轉(zhuǎn)化為能包含它大小的最小如下類型:int腔寡,unsigned int焚鲜,long,unsigned long放前。
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n226" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> short chickens = 20; //line 1
short ducks =35; //line 2
short fowl = chickens + ducks; //line 3</pre>
在執(zhí)行第三條語(yǔ)句時(shí)忿磅,計(jì)算機(jī)傾向于先將chickens和ducks轉(zhuǎn)化為int型,求得結(jié)果后在轉(zhuǎn)化為short型賦值給fowl變量凭语。因?yàn)閷?duì)CPU而言葱她,int型是計(jì)算效率最快的一種數(shù)據(jù)類型。
簡(jiǎn)而言之似扔,當(dāng)表達(dá)式存在兩種不同類型的數(shù)據(jù)進(jìn)行運(yùn)算時(shí)吨些,數(shù)值范圍小的將自動(dòng)向數(shù)值范圍大的轉(zhuǎn)化:
- 傳參時(shí)的轉(zhuǎn)換:一般來(lái)說(shuō),參數(shù)的類型轉(zhuǎn)換由函數(shù)原型中聲明的進(jìn)行炒辉,但C++將自動(dòng)轉(zhuǎn)換char锤灿,short(包含signed和unsigned)型的參數(shù)為int型。同時(shí)為了兼容經(jīng)典C辆脸,自動(dòng)將float轉(zhuǎn)化成double但校。
10.4 強(qiáng)制類型轉(zhuǎn)換
在C++中有多種形式的強(qiáng)制類型轉(zhuǎn)換:(以將int型的thorn變量轉(zhuǎn)化為long型為例)
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n235" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> (long)thron //C語(yǔ)言形式
long(thron) //C++形式,使之看起來(lái)像函數(shù)調(diào)用
static_cast<long> (thron) //</pre>
強(qiáng)制類型轉(zhuǎn)換并不實(shí)際改變?cè)兞康念愋头惹猓皇巧梢粋€(gè)臨時(shí)數(shù)值状囱。