和C的创建方式相同


1,单行注释://
2,多行注释:/* */
作用:给一段指定的内存空间起名,方便操作这段内存
语法:数据类型 变量名 = 初始值
- #include<iostream>
- using namespace std;
-
- int main()
- {
- //变量创建的语法:数据类型 变量名 = 变量初始值
- int a = 10;//与句代码与c语言一样
- cout << "a = " << a << endl;//输出
- system("pause");
- return 0;
- }

作用:用于记录程序中不可更改的数据
1,#define宏定义 #define 常量名 常量值
*通常在文件上方定义,表示一个常量
示例:
- //#define宏定义
- #define day 7
- int main()
- {
- day = 14;//错误,day是常量,一旦修改就会报错
- cout << "一周总共有:" << day << "天" << endl;
- system("pause");
- return 0;
- }
2,const修饰的变量 const 数据类型 常量名 = 常量值
*通常在变量定义前加关键字const,修饰该变量为常量,不可修改
示例:
- int main()
- {
- const int month = 12;
- //month=24;//报错,常量是不可修改的
- cout << "一年总共有:" << month <<"个月份" << endl;
- system("pause");
- return 0;
- }
作用:关键字是C++中预先保留的单词
在定义变量或者常量时候,不要用关键字
作用:C++规定给标识符(变量,常量)命名时,有一套自己的规则
标识符不能是关键字
标识符只能由字母,数字,下划线组成
第一个字符必须为字母或下划线
标识符中字母区分大小写

- int main()
- {
- //整型
- //1,短整型(-32768~32767)
- short num1 = 32768;
-
- //2,整型
- int num2 = 20;
-
- //3,长整型
- long num3 = 30;
-
- //4,长长整型
- long long num4 = 40;
-
- cout << "num1= " << num1 << endl;
- cout << "num2= " << num2 << endl;
- cout << "num3= " << num3 << endl;
- cout << "num4= " << num4 << endl;
-
- system("pause");
- return 0;
- }

作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
示例:
- int main()
- {
- cout << "short 类型所占的内存空间为:" << sizeof(short) << endl;
- cout << "int 类型所占的内存空间为:" << sizeof(int) << endl;
- cout << "long 类型所占的内存空间为:" << sizeof(long) << endl;
- cout << "long long 类型所占的内存空间为:" << sizeof(long long) << endl;
- system("pause");
- return 0;
- }

整型字节长度结论:short
还有一种表示方法:
- short num1 = 10;
- cout << "short 类型所占的内存空间为:" << sizeof(num1) << endl;

作用:表示小数
浮点型变量分两种:
1,单精度型float
2,双精度型double
二者的区别是读取的范围不同:
float 占4个字节 7位有效数字
double 占8个字节 15~16位有效数字
- int main()
- {
- float f1 = 3.14f;
- cout << "f1 = " << f1 << endl;
- double d1 = 3.14;
- cout<<"f2= " << d1 << endl;
-
- //统计float和double占用内存空间
- cout << "float 类型所占的内存空间为:" << sizeof(float) << endl;
- cout << "double 类型所占的内存空间为:" << sizeof(double) << endl;
-
- //科学计数法
- float f2 = 3e2;//3*10^2
- cout << "f2=" << f2 << endl;
-
- float f3 = 3e-2;//3*0.1^2
- cout << "f3=" << f3 << endl;
- system("pause");
- return 0;
- }

作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注意:在显示字符型变量时,用单引号将字符括起来,不要用双引号
单引号内只能有一个字符,不可以是字符串
C和C++中字符型变量只占用1个字节
字符型变量并不是把字符型本身放到内存中存储,而是将对应的ASCII编码放入存储单元

- int main()
- {
- //1,字符型变量创建方式
- char ch = 'a';
- cout << ch << endl;
- //2,字符型变量所占内存大小
- cout << "char字符型变量所占内存:" << sizeof(char)<<endl;
- //3,字符型常见错误
- //char ch2 = "b";//创建字符型变量时,要用单引号
- //char ch3 = 'abcdrf';//创建字符型变量时,单引号内只能有一个字符
- //4,字符型变量对应ascii编码
- cout << (int)ch << endl;//查看字符a对应的ASCII码
- ch = 97;//可以直接用ASCII给字符型变量赋值
- cout << ch << endl;
- system("pause");
- return 0;
- }

作用:用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有:\n,\\,\t


作用:用于表示一串字符
两种风格
1,C风格字符串 "char 变量名[ ] = "字符串值“
示例:
- int main()
- {
- char str[] = "hello world";
- cout << str << endl;
-
- system("pause");
- return 0;
- }
2,c++风格字符串 string 变量名 = ”字符串值“
- string str2 = "hello world";
- cout << str2 << endl;
作用:布尔数据类型代表真或假的值
bool只有两个值:
ture—真(本质是1)
false----假(本质是0)
bool占一个字节大小
- int main()
- {
- //创建bool数据类型
- bool flag = true;
- cout << flag << endl;
-
- flag = false;
- cout << flag << endl;
-
- //本质上1代表真,0代表假
-
- //查看bool类型所占内存空间
- cout << "size of bool:" <<sizeof(bool)<< endl;//1
-
- system("pause");
- return 0;
- }
