天天看點

C++從0到1(2):資料類型

目錄

    • 1. 整型
    • 2. sizeof關鍵字
    • 3. 實型(浮點型)
    • 4. 字元型
    • 5. 轉義字元
    • 6. 字元串型
    • 7. 布爾類型
    • 8. cin(資料的輸入)
  • C++規定在建立一個變量或常量時,必須要指定相應的資料類型,否則無法給變量配置設定記憶體。
  • 資料類型存在的意義:給變量配置設定一個合适的記憶體空間

1. 整型

  • 作用: 整型變量表示整數類型的資料
  • 類型:
資料類型 占用空間 取值範圍
short (短整型) 2位元組 − 2 15 -2^{15} −215 ~ 2 15 2^{15} 215-1(-32768–32767)
int (整型) 4位元組 − 2 31 -2^{31} −231 ~ 2 31 2^{31} 231-1
long (長整型) Win4位元組,Linux:32位4位元組,64位8位元組 − 2 31 -2^{31} −231 ~ 2 31 2^{31} 231-1
longlong(長長整型) 8位元組 − 2 62 -2^{62} −262 ~ 2 62 2^{62} 262-1
#include<iostream>
using namespace std;

int main()
{
	//1、短整型
	short num1 = 32768;//輸出-32768
	//2、整型
	int num2 = 32768;
	//3、長整型
	long num3 = 10;
	//4、長長整型
	long long num4 = 10;

	cout << "num1 = " << num1 << endl; 
	cout << "num2 = " << num2 << endl;
	cout << "num3 = " << num3 << endl;
	cout << "num4 = " << num4 << endl;
	system("pause");
	return 0;
}
           

2. sizeof關鍵字

  • 作用: 利用sizeof關鍵字統計資料類型所占記憶體大小
  • 文法: sizeof(資料類型/變量)
#include<iostream>
using namespace std;

int main()
{
	//文法:sizeof(資料類型/變量)
	short num1 = 10;
	cout << "num1 size:" << sizeof(num1) << endl;
	cout << "short size:" << sizeof(short) << endl;
	int num2 = 2;
	cout << "int size:" << sizeof(int) << endl;
	long num3 = 4;
	cout << "long size:" << sizeof(long) << endl;
	long long num4 = 70;
	cout << "long long size:" << sizeof(long long) << endl;
	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

3. 實型(浮點型)

  • 作用: 表示小數
  • 類型:

            單精度float:4位元組,7位有效數字

            雙精度double:8位元組,15~16位有效數字

#include<iostream>
using namespace std;

int main()
{
	//單精度:float
	//雙精度:double
	//編譯器預設小數位雙精度,需要加f,
	//否則還要把雙精度轉換為單精度
	float f1 = 3.1415926f;
	cout << "f1:" << f1 << endl;
	double d1 = 3.1415926;
	cout << "d1:" << d1 << endl;
	//預設情況下,輸出一個小數,會顯示出6位有效數字

	cout << "float size:" << sizeof(float) << endl;
	cout << "double size:" << sizeof(double) << endl;

	//科學計數法表示小數
	float f2 = 3e2;//3*10^2
	cout << "f2:" << f2 << endl;
	float f3 = 3e-2;//3*0.1^2

	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

4. 字元型

  • 作用: 字元型變量用于顯示單個字元
  • 文法: char ch = ‘a’; 單引号、單引号内隻有單個字元

C++和C中字元型變量隻占1個位元組

字元型變量是将對應的ASCII編碼(對應的編号)放入儲存單元

eg:a — 97、b — 98

#include<iostream>
using namespace std;

int main()
{
	//1、字元型變量建立方式
	char ch1 = 'a';
	cout << "ch1:" << ch1 << endl;
	//2、字元型變量所占記憶體空間大小
	cout << "char size:" << sizeof(char) << endl;
	//3、字元型變量常見錯誤
	//char ch2 = "b";//雙引号報錯
	//char ch2 = 'abc';//多個字元報錯
	//4、字元型變量對應ASCII編碼
	//強制轉為整型
	cout << int(ch1) << endl;
	//a:97   A:65
	system("pause");
	return 0;
}
           

5. 轉義字元

  • 作用: 用于表示一些不能顯示出來的ASCII字元
  • 常用: \ \n \t
    C++從0到1(2):資料類型
#include<iostream>
using namespace std;

int main()
{
	//轉義字元

	//換行符\n
	cout << "hello word\n" << endl;
	//反斜杠
	cout<<"\\"<<endl;
	//水準制表符\t   可以整齊的輸出資料
	cout << "aaa\thello" << endl;//aaa加空格一共8個字元
	cout << "aaaaa\thello" << endl;
	cout << "aaaaaa\thello" << endl;
	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

6. 字元串型

  • 作用: 表示一串字元
  • 風格:

        C風格字元串:char 名稱[] = “字元串”;

        C++風格字元串:string 名稱 = “字元串”;

#include<iostream>
using namespace std;
#include<string>//用C++風格寫字元串,要包含這個頭檔案

int main()
{
	//C風格
	//注意:中括号、雙引号
	char str1[] = "hello word";
	cout << "str1:"<<str1 << endl;
	//C++風格
	//#include<string>//用C++風格寫字元串,要包含這個頭檔案
	string str2 = "hi excel";
	cout << "str2:" << str2 << endl;
	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

7. 布爾類型

  • 作用: 代表真或假的值(占1個位元組大小)
  • true:1
  • false:0
#include<iostream>
using namespace std;

int main()
{
	//1、建立布爾類型資料
	bool flag = true;//代表1
	cout << flag << endl;
	
	flag = false;//代表0
	cout << flag << endl;
	//2、檢視布爾類型所占記憶體空間
	cout << "bool size:" << sizeof(bool) << endl;

	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

8. cin(資料的輸入)

  • 作用: 鍵盤輸入
#include<iostream>
using namespace std;
#include<string>//用C++風格寫字元串,要包含這個頭檔案

int main()
{
	//1、整型輸入
	int a = 1;
	cout <<"give a a value:" << endl;
	cin >> a;
	cout << "a value is:" << a << endl;
	
	//2、浮點型
	float f = 3.14f;
	cout << "give f a value:" << endl;
	cin >> f;
	cout << "f value is:" << f << endl;
	
	//3、字元型
	char ch = 'a';
	cout << "give ch a value:" << endl;
	cin >> ch;
	cout << "ch value is:" << ch << endl;
	
	//4、字元串型
	string str = "abc";
	cout << "give str a value:" << endl;
	cin >> str;
	cout << "str value is:" << str << endl;
	
	//5、布爾型
	bool flag = true;
	cout << "give flag a value:" << endl;
	cin >> flag;
	cout << "flag value is:" << flag << endl;

	system("pause");
	return 0;
}
           
C++從0到1(2):資料類型

繼續閱讀