1. 程式人生 > >C++ Primer Plus 第六版學習筆記第三章

C++ Primer Plus 第六版學習筆記第三章

 

1、檢視系統中各資料型別所佔的位元組數(sizeof),所能表示的最大和最小取值,標頭檔案climite中包含了關於整型限制的資訊,定義了所使用的各種符號常量

例:檢視各種整型資料型別所佔的位元組數以及所能表示的最大數值

#include "stdafx.h"
#include <iostream>
#include<climits>
using namespace std;

int main()

{

	int n_lit = INT_MAX;
	short n_short = SHRT_MAX;
	long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;
	//得到每種資料型別所能表示的最大取值
	cout << "int is " << sizeof(int) << " bytes." << endl;
	cout << "short is " << sizeof(short) << " bytes." << endl;
	cout << "long is " << sizeof(long) << " bytes." << endl;
	cout << "long long is " << sizeof n_llong << " bytes." << endl;
//輸出每種資料型別的位數
	cout << "maximum values:" << endl;
	cout << "int: " << n_lit << endl;
	cout << "short: " << n_short << endl;
	cout << "long: " << n_long << endl;
	cout << "long long: " << n_llong << endl;
//輸出每種資料型別的最大取值
	cout << "Minimum int value = " << INT_MIN << endl;//其他資料型別也可用類似的方法得到最小取值
	cout << "Minimum short value = " << SHRT_MIN << endl;
	cout << "Bite per byte = " << CHAR_BIT << endl;//CHAR_BIT為位元組的位數
	return 0;
}

2、

2、c++特有的初始化

   int a=(0)、int a(0)、int a={0}、int a{}、int a{}等價於int a=0;大括號前的等號可以不要,空的大括號預設為初始化為0,不能使用空的中括號進行初始化;大括號型別的初始化可以用於任何型別;對於大括號的初始化,可能會存在型別的轉化。

3、

3、c++三種不同計數方式表示(c中相同表示方法)

   使用前1、2位來標識數字常量的基數,若第一位為1-9整數表示十進位制;若第一位為0,第二位為1-7則表示8進位制;若1、2位為0x/0X,則表示16進位制;例:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()

{
	int cheast = 42;
	int waist = 0x42;
	int inseam = 042;
	cout << "cheast = " << cheast << endl;//cout預設為十進位制輸出
	cout << "waist = " << waist << endl;
	cout << "inaeam = " << inseam << endl;
	return 0;
}

 

4、cout以不同進位制格式顯示整數

   Cout預設為十進位制格式顯示整數,標頭檔案iostream定義了控制符dec、hex、oct分別用於指示用十進位制、十六進位制、八進位制顯示整數。

例:

#include<stdafx.h>
#include <iostream>
using namespace std;

int main()

{
	int cheast = 42;
	int waist = 0x42;
	int inseam = 042;
	cout << "cheast = " << cheast << endl;//cout預設為十進位制輸出
	cout << "waist = " << waist << endl;
	cout << "inaeam = " << inseam << endl;
	cout << hex;//此行之後用16進製表示整數
	cout << "cheast = " << cheast << endl;
	cout << "waist = " << waist << endl;
	cout << "inaeam = " << inseam << endl;
	cout << oct;//此行之後用8進製表示整數
	cout << "cheast = " << cheast << endl;
	cout << "waist = " << waist << endl;
	cout << "inaeam = " << inseam << endl;
	cout << dec;//改為用十進位制表示整數,在用過其他進位制的控制符後,若想要改回十進位制需要有此行說明
	cout << "cheast = " << cheast << endl;//cout預設為十進位制輸出
	cout << "waist = " << waist << endl;
	cout << "inaeam = " << inseam << endl;
	return 0;
}

5、

c++中常量的儲存型別

1)、整型常量:一般在不超過int型最大數值時,預設存為int,可在常量後加字尾表示存為其他型別。Longlong-ll/LL,unsigned long long-ull/ULL/uLL/Ull,unsigned int-ul/UL/Ul,long-l/L

對於不帶字尾的十進位制數將使用int、long、longlong中能儲存該數的最小型別表示;對於不帶字尾的十六、八進位制數將使用int、unsigned int、unsigned long、long long、unsigned long long中能儲存該數的最小型別表示

2)、浮點型常量:預設為double型別,可用字尾f/F,l/L表示存為float或long double型別

6、新增的資料型別(對c)

1)、char16_t與char32_t

   兩種型別的字元變數均是無符號型別的,前者為16位且用字首u表示該型別的字元常量和字串常量,後者為32位且用字首U表示。

  2)、bool

     屬於整型的一種,但只有ture、false兩種,分別表示非零值與零值。

7、const

   const int Month=12等價於#define Month 12

8、強制型別轉換

 C:(int)20.9,表示將20.9轉換為整型即20;c++:int(20.9),表示將20.9轉換為整型即20.即c和c++風格的強制型別轉換括號位置不同。