數據類型
C++規定在創建一個變量或者常量時,必須要指定相應的數據類型,否則無法給變量分配內存空間。
01 整型:
數據類型 | 占用空間 | 取值范圍 |
short(短整型) | 2字節 | -2^15~2^15-1 |
int(整型) | 4字節 | -2^31~2^31-1 |
long(長整型) | 4字節/8字節 | -2^31~2^31-1 |
long long(長長整型) | 8字節 | -2^63~2^63-1 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<iostream> using namespace std; int main1() { //整型 //1.短整型 short num1 = 10; //short num1 = 32768; //2.整型 int num2 = 10; //3.長整型 long num3 = 10; //4.長長整型 long long num4 = 10; cout << num1 << endl; cout << num2 << endl; cout << num3 << endl; cout << num4 << endl; system ( "pause" ); return 0; } |
02 sizeof關鍵字
作用:利用sizeof關鍵字可以統計數據類型所占內存大小。
語法:sizeof
(數據類型/變量)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include<iostream> using namespace std; int main2() { //整型:short(2) int(4) long(4) long long(8) //可以利用sizeof求出數據類型占用內存大小 //語法:sizeof(數據類型/變量) short num1 = 10; cout << "short占用內存空間為:" << sizeof ( short ) << endl; //cout << "short占用內存空間為:" << sizeof(num1) << endl; int num2 = 10; cout << "int占用內存空間為:" << sizeof ( int ) << endl; //cout << "int占用內存空間為:" << sizeof(num2) << endl; long num3 = 10; cout << "long占用內存空間為:" << sizeof ( long ) << endl; //cout << "long占用內存空間為:" << sizeof(num3) << endl; long long num4 = 10; cout << "long long占用內存空間為:" << sizeof ( long long ) << endl; //cout << "long long占用內存空間為:" << sizeof(num4) << endl; //整型大小比較 //short < int <= long <= long long system ( "pause" ); return 0; } |
03 實型(浮點型)
作用:用于表示小數。
浮點型變量分為兩種:float和double,區別在于表示的數字范圍不同。
數據類型 | 占用空間 | 有效數字范圍 |
float | 4字節 | 7位有效數字 |
double | 8字節 | 15~16位有效數字 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include<iostream> using namespace std; int main3() { //實型 //1.單精度 float //2.雙精度 double //默認情況下,輸出一個小數,會顯示出6位有效數字 float f1 = 3.1415926f; double d1 = 3.1415926; cout << "f1 = " << f1 << endl; cout << "d1 = " << d1 << endl; //float占用4字節內存空間,double占用8字節 cout << "float占用的內存空間為:" << sizeof ( float ) << endl; cout << "double占用的內存空間為:" << sizeof ( double ) << endl; //科學計數法 float f2 = 3e2; //3*10^2,300 cout << "f2 = " << f2 << endl; float f3 = 3e-2; //3*0.1^2,0.03 cout << "f3 = " << f3 << endl; system ( "pause" ); return 0; } |
04 字符型
作用:用于顯示單個字符。
語法:char ch = 'a';
注意1:在顯示字符串變量時,用單引號將字符括起來,不要用雙引號。
注意2:單引號內只能有一個字符,不可以是字符串。
C和C++中字符串變量只占用1個字節。
字符型變量并不是把字符本身放到內存中存儲,而是將對應的ASCII編碼放到存儲單元中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<iostream> using namespace std; int main4() { //1.字符型變量創建方式 char ch = 'a' ; cout << ch << endl; //2.字符型變量所占內存大小 cout << "char字符型變量所占內存為:" << sizeof ( char ) << endl; //3.字符型變量常見錯誤 //char ch2 = "b"; 創建字符型變量時,要用單引號 //char ch2 = 'abcdefg'; 創建字符型變量時,單引號內只能有一個字符 //4.字符型變量對應ASCII編碼 //a - 97 A - 65 cout << ( int )ch << endl; system ( "pause" ); return 0; } |
05 轉義字符
作用:用于表示一些不能顯示出來的ASCII字符。
\n 換行,將當前位置移到下一行開頭
\t 水平制表,跳到下一個TAB位置
\\ 代表一個反斜線字符'\'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<iostream> using namespace std; int main5() { //轉義字符 //換行符\n cout << "Hello world" << endl; cout << "Hello world\n" ; //反斜杠\\ cout << "\\" << endl; //水平制表符\t cout << "aaaa\thelloworld" << endl; cout << "aa\thelloworld" << endl; cout << "aaaaaa\thelloworld" << endl; system ( "pause" ); return 0; } |
06 字符串型
C風格字符串:char 變量名[] = "字符串值"
C++風格字符串:string 變量名 = "字符串值"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include<iostream> #include<string> //用C++風格字符串時,要包含這個頭文件 using namespace std; int main6() { //1.C風格字符串 //注意事項1:char字符串名后加[] //注意事項2:等號后面要用雙引號,包含起來字符串 char str[] = "hello world" ; cout << str << endl; //2.C++風格字符串 string str2 = "hello world" ; cout << str2 << endl; system ( "pause" ); return 0; } |
07 布爾型
作用:布爾數據類型代表真或者假的值。
只有兩個值:True或者False,占用1字節大小的內存空間。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include<iostream> using namespace std; int main7() { //1.創建bool數據類型 bool flag = true ; cout << flag << endl; flag = false ; cout << flag << endl; //1代表真,0代表假 //2.查看bool類型所占內存空間 cout << "bool類型所占內存空間:" << sizeof ( bool ) << endl; system ( "pause" ); return 0; } |
08 數據的輸入
作用:從鍵盤獲取數據。
關鍵字:cin
語法:cin >> 變量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include<iostream> #include<string> using namespace std; int main() { //1.整型 /*int a = 0; cout << "請給整型變量a賦值:" << endl; cin >> a; cout << "整型變量a = " << a << endl;*/ //2.浮點型 /*float f = 3.14f; cout << "請給浮點型變量f賦值:" << endl; cin >> f; cout << "浮點型變量f = " << f << endl;*/ //3.字符型 /*char ch = 'a'; cout << "請給字符型變量ch賦值:" << endl; cin >> ch; cout << "字符型變量ch = " << ch << endl;*/ //4.字符串型 /*string str = "hello"; cout << "請給字符串型變量str賦值:" << endl; cin >> str; cout << "字符串型變量str = " << str << endl;*/ //5.布爾型 bool flag = false ; cout << "請給布爾型變量flag賦值:" << endl; cin >> flag; cout << "布爾型變量flag = " << flag << endl; system ( "pause" ); return 0; } |
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/weixin_43943624/article/details/121449047