1、变量
语法:数据类型 变量名=变量初始值;
例:int a = 10;(整型)
#include<iostream>
using namespace std;
int main()
{
//变量创建的语法:数据类型 变量名 = 变量初始值;
int a=10;
cout<<"a="<<a<<endl;
system("pause");
return 0;
}
2、常量的定义方式:
(1)#define宏常量:#define 常量名 常量值
通常在文件上方定义,表示一个常量
(2)const修饰的变量:const 数据类型 常量名=常量值
通常在变量定义前加关键字const,修饰该变量为常量,不可修改,通常在函数内部
#include<iostream>
using namespace std;
//常量定义的方式
//1、#define 宏常量,通常在函数上方
#define Day 7
int main()
{
//Day = 14;//错误,Day是常量,一旦修改就会报错
cout<<"一周总共有:"<<Day<<"天"<<endl;
//2、const修饰的变量
const int month=12;
//month=24;//错误,const修饰的变量也称为常量
cout<<"一年总共有:"<<month<<"个月份"<<endl;
system("pause");
return 0;
}
3、关键字(标识符)
asm | do | if | return | typedef |
auto | double | inline | short | typeid |
bool | dynamic_cast | int | signed | typename |
break | else | long | sizeof | union |
case | enum | mutable | static | unsigned |
catch | explicit | namespace | static_cast | using |
char | export | new | struct | virtual |
class | extern | operator | switch | void |
const | false | private | template | volatile |
const_cast | float | protected | this | wchar_t |
continue | for | public | throw | while |
default | friend | register | true | |
delete | goto | reinterpret_cast | try |
提示:在定义变量或者常量的时候不能使用关键字
4、标识符命名规则
a、标识符不能是关键字
b、标识符只能由字母、数字、下划线组成
c、第一个字符必须为字母或下划线
d、标识符中字母区分大小写
#include<iostream>
using namespace std;
//标识符命名规则
//a、标识符不能是关键字
//b、标识符只能由字母、数字、下划线组成
//c、第一个字符必须为字母或下划线
//d、标识符中字母区分大小写
int main()
{
//a、标识符不能是关键字
//int int = 10;
//b、标识符只能由字母、数字、下划线组成
int abc = 10;
int _abc = 20;
int _123abc = 30;
//c、第一个字符必须为字母或下划线
//int 123abc = 0;
//d、标识符中字母区分大小写
int a = 1;
//cout<<A<<endl;A和a不是同一个名称
system("pause");
return 0;
}