基本变量类型
C++ has some original variable type, called seven basic variable.
C++有七个基本变量类型
且C++是面向对象的编程语言,可以对基本变量类型进行组合,也可自己构造自己的数据结构。
/*
* Time: 2021-1-15
* Author: Fei Dongxu <Phil616@163.com>
*/
#include <iostream>
int main(){
int IntegerType;
bool BooleanType;
char CharacterType;
float FloatingType;
double DoubleFloatingType;
void NullType;
wchar_t WideCharacterType;
//typedef short int wchar_t;
//This is how the wchar_t come from.
return 0;
}
算术类型
整型
浮点型
修饰符类型
/*
Some of the key words can be complex together.
like the program below:
*/
unsigned long int
long long int
unsigned short
/*
To understand the limit size of these types, we can use headfile <limits.h> to study.
*/
类型大小
This is a example which runoob.com given to us.
#include<iostream>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "Number of bytes: " << sizeof(bool);
cout << "\tMax:" << (numeric_limits<bool>::max)();
cout << "\t\tMin:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "Number of bytes: " << sizeof(char);
cout << "\tMax:" << (numeric_limits<char>::max)();
cout << "\t\tMin:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "Number of bytes: " << sizeof(signed char);
cout << "\tMax:" << (numeric_limits<signed char>::max)();
cout << "\t\tMin:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "Number of bytes: " << sizeof(unsigned char);
cout << "\tMax:" << (numeric_limits<unsigned char>::max)();
cout << "\t\tMin:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "Number of bytes: " << sizeof(wchar_t);
cout << "\tMax:" << (numeric_limits<wchar_t>::max)();
cout << "\t\tMin:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "Number of bytes: " << sizeof(short);
cout << "\tMax:" << (numeric_limits<short>::max)();
cout << "\t\tMin:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "Number of bytes: " << sizeof(int);
cout << "\tMax:" << (numeric_limits<int>::max)();
cout << "\tMin:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "Number of bytes: " << sizeof(unsigned);
cout << "\tMax:" << (numeric_limits<unsigned>::max)();
cout << "\tMin:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "Number of bytes: " << sizeof(long);
cout << "\tMax:" << (numeric_limits<long>::max)();
cout << "\tMin:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "Number of bytes: " << sizeof(unsigned long);
cout << "\tMax:" << (numeric_limits<unsigned long>::max)();
cout << "\tMin:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "Number of bytes: " << sizeof(double);
cout << "\tMax:" << (numeric_limits<double>::max)();
cout << "\tMin:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "Number of bytes: " << sizeof(long double);
cout << "\tMax:" << (numeric_limits<long double>::max)();
cout << "\tMin:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "Number of bytes: " << sizeof(float);
cout << "\tMax:" << (numeric_limits<float>::max)();
cout << "\tMin:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "Number of bytes: " << sizeof(size_t);
cout << "\tMax:" << (numeric_limits<size_t>::max)();
cout << "\tMin:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "Number of bytes: " << sizeof(string) << endl;
// << "\tMax:" << (numeric_limits<string>::max)() << "\tMin:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
它的输出结果是:
type: ************size**************
bool: Number of bytes: 1 Max:1 Min:0
char: Number of bytes: 1 Max: Min:€
signed char: Number of bytes: 1 Max: Min:€
unsigned char: Number of bytes: 1 Max: Min:
wchar_t: Number of bytes: 2 Max:65535 Min:0
short: Number of bytes: 2 Max:32767 Min:-32768
int: Number of bytes: 4 Max:2147483647 Min:-2147483648
unsigned: Number of bytes: 4 Max:4294967295 Min:0
long: Number of bytes: 4 Max:2147483647 Min:-2147483648
unsigned long: Number of bytes: 4 Max:4294967295 Min:0
double: Number of bytes: 8 Max:1.79769e+308 Min:2.22507e-308
long double: Number of bytes: 8 Max:1.79769e+308 Min:2.22507e-308
float: Number of bytes: 4 Max:3.40282e+38 Min:1.17549e-38
size_t: Number of bytes: 4 Max:4294967295 Min:0
string: Number of bytes: 28
type: ************size**************
有一些其他类型的数据在不同的环境下不一致,可用通过循环来看一看他们的最大值和最小值
/*
* Time: 2021-1-15
* Author: Fei Dongxu <Phil616@163.com>
*/
#include<iostream>
using namespace std;
typedef short testedType;
//We can change int to any other type.
int main()
{
testedType test = 0;
int max = 0;
int min = 0;
int flag = 1;
while (1){
test++;
if(max < test) max = test;
if(min > test) min = test;
cout << test << " max is " << max << " min is " << min << endl;
}
return 0;
}
//This is a dead loop, we have to stop it in console or debug toolbox.
//while the min and max is no longer change.
类型选择
算术表达式中不要选择char和bool,因为char有时候有符号有时候无符号
只有确认不会涉及负数的时候才使用unsigned类型
优先使用double,float和double的区别不大,但是long double类型不必要
脱离环境行为
在一些不同的机器上运行程序需要考虑环境的问题,编译器和系统环境不同会导致变量的尺寸不一致,需要确定一个在其他编译器或其他环境都不变的变量尺寸才能确保可移植性。
无符号
对于无符号的运算需要参考CSAPP上的实验和算法,日常使用只需要进行测试即可。
字面值变量
转义序列
C++和C都支持一些来源于系统的转义序列
换行符 \n
横向制表符 \t
报警响铃符 \a
纵向制表符 \v
退格符 \b
单引号 \'
双引号 \"
反斜线 \\
问号 \?
回车符 \r
进纸符 \f
转义序列也可以进行一些泛化,例如下面这类可以表示一般字符的情况
\7 响铃
\0 空字符
\12 换行符
\40 空格
\115 字母M
\x4d 字母M
指定字面值的类型
如果添加一些前缀和后缀会改变字面值本来的类型
例如
L'a' //wchar_t的宽字符型字面值
u8"Hi!" //utf8字符串,用8位编码一个Unicode字符
42ULL //Unsigned long long 类型
1E-3F //科学计数法的单精度float
3.14159L //long double类型
需要注意long类型的L需要大写,虽然小写符合规则,但是由于1和l的相似性容易失误。
字符和字符串字面值
前缀 | 含义 | 类型
u Unicode 16字符 char16_t
U Unicode 32字符 char32_t
L 宽字符 wchar_t
u8 UTF-8(仅字符串) char
整型字面值
后缀 | 最小的匹配类型
u/U unsigned
l/L long
ll/LL long long
浮点型字面值
f/F float
l/L long double
初始化
当我们使用变量时应该进行初始化。
//This is the usual way to initialize variables
int i = 0;
float f = 0;
double d = 0;
char c = '\0';
int *p = NULL;
定义和初始化的顺序是从左到右,那么可以在同一行进行定义和初始化的操作
double pi = 3.14,d = 2*pi*2;
//pi先被定义,然后用于初始化d
列表初始化
C++提供了多种初始化的方式,使用花括号初始值的方式属于列表初始化。
如果列表初始化在一开始就有丢失数据的风险,编译器会报错。
int i = 0;
int i = {0};
int i{0};
int i(0);
如果变量不被初始化,则进行使用,那么可能会产生意想不到的错误或编译器报错和警告。
声明和定义
extern可以进行声明,且可以进行多次声明,但是一旦函数被定义,则不可重定义
变量的作用域
#include <iostream>
using namespace std;
int g_var01 = 10;
int main(){
int var01 = 20;
return 0;
}
void func(){
int var02 = 30;
}
//g_var01 can be used at all functions
//var01 and var02 can only be used at local functions.
常量
100 is a constant data.
“This is a String” is also a constant data.
const double pi = 3.14;
pi = 3.0;//The complier will report an error. caz u cannot change a const int type.