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++风格的强制类型转换括号位置不同。

 

猜你喜欢

转载自blog.csdn.net/Schlangemm/article/details/83306121