【variable】
Conveniently manage memory space
【type of data】
Allocate reasonable memory space to variables
Data type variable name = variable initial value;
int a = 12;
【constant】
The amount that cannot be modified after initialization
The first type: define constants
The second type: variables defined by const
const int a = 12;
a = 23; //报错
[Identifier naming principle]
- Cannot be a keyword
- Must be composed of letters, underscores, and numbers
- It cannot start with a number [just an underscore is also OK]
- Strictly control case
[Byte judgment of data structure (sizeof)]
Use syntax:
sizeof (data structure/variable name)
short <int <= long <= long long
[long has a different number of bytes in different operating systems]
#include <iostream>
using namespace std;
int main()
{
int num = 3;
//计算内存空间
/* 可以使用数据类型或者变量作为sizeof的内容 */
cout << "int 型的内存空间为: " << sizeof(int) << endl;
cout << "int 型的内存空间为: " << sizeof(num) << endl;
return 0;
}
[Principles of using scientific notation in float]
e or E stands for 10
int main()
{
//科学计数法
float a1, a2;
a1 = 3e2;//a1 = 3 * 10 ^ 2
cout << "a1 = " << a1 << endl;
a2 = 3e-2;//a2 = 3 * 0.1 ^ 2 = 3 * 10 ^ (-2)
cout << "a2 = " << a2 << endl;
return 0;
}
[Use char type to output the value of the ASCII table]
#include <iostream>
using namespace std;
int main()
{
//错误使用:
//error 1 :char ch = "a"; 创建字符型变量时,必须用单引号
//error 2 :char ch = 'abdffdd'; 创建字符型变量时,单引号内只能是一个字符
char ch = 'a';
cout << (int)ch << endl;//输出97
cout << ch << endl;//输出a
//字符型常用
//a--97
//A--65
//相差32
return 0;
}
[Escape character--horizontal tab \t]
#include <iostream>
using namespace std;
int main()
{
// \t 水平制表符号
//一个\t占据8个字节空间,使用\t可以控制整齐度
cout << "aaa\thello world!\n";
cout << "aaaaa\thello world!" << endl;//\n等价于endl(换行字符)
cout << "aa\thello world!\n";
return 0;
}
//转义字符:俩个字母构成,第一个字符必为 \ ;
Escape character: