c++基本数据类型之整形及赋值方法

#include <iostream>
// climits 是由c里的limits.h转化成c++可用的limits前面的c表示是从c的头文件过来的,所以叫climits
#include <climits>
using namespace std;


int main() 
{
	// 获取各个整形的最大值
	int n_int = INT_MAX;
	short n_short = SHRT_MAX;
	long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;
	
	cout << " int max : " << n_int << endl;
	cout << " short max : " << n_short << endl;
	cout << " long max : " << n_short << endl;
	cout << " long long max : " << n_llong << endl;
	
	// 通过sizeof 关键字能够获取int,short,long,long long等数据类型的大小
	// sizeof + 变量, 也可以写成 sizeof(int) 这种格式
	cout << " size of int " << sizeof n_int << "字节, 也可以这么写" << sizeof(int) << endl;
	cout << " size of short " << sizeof n_short << "字节, 也可以这么写" << sizeof(short) << endl;
	cout << " size of long " << sizeof n_long << "字节, 也可以这么写" << sizeof(long) << endl;
	cout << " size of long long " << sizeof n_llong << "字节, 也可以这么写" << sizeof(long long) << endl;
	
	// c++赋值语句, 一下几种赋值语句都可以
	int n_age = 10;
	// 之所以新添加{}初始化方式, 是因为想使得初始化常规变量方式与初始化类变量的方式更像
	int n_age2 = {20};
	// 也可以通过()赋值
	int n_age3(30);
	// 如果{}里面没有任何数值, 则默认为0
	int n_age4 = {};
	int n_age5{};
	cout << "n_age = " << n_age << endl;
	cout << "n_age2 = " << n_age2 << endl;
	cout << "n_age3 = " << n_age3 << endl;
	cout << "n_age4 = " << n_age4 << endl;
	cout << "n_age5 = " << n_age5 << endl;
}

需要注意一点, c++里的各个整数类型的大小在不同的系统中可能不同, 所以具体的编译环境中查看所占字节数需要使用sizeof关键字查看, 

运行结果如下:

猜你喜欢

转载自blog.csdn.net/c1392851600/article/details/83902429