编写C++四个步骤:
创建项目 创建文件 编写代码 运行程序
例码:
#include<iostream>
using namespace std;
int main()//main 是一个程序的入口 每个程序都要有这么一个入口 有且只有一个
{
cout《“ hello world”《endl;//输出 hello world
system(“pause”);
return 0;
}
注释:
单行注释://
多行注释:/* */
变量:存在的意义 方便我们管理内存空间
变量创建的语法:数据类型 变量名=变量初始值;
int a =10;
例:int a =10;
cout<<"a="<<a<<endl;//输出a=10
常量:用于记录程序中不可更改的数据
定义常量的两种方式:
1.#define宏常量: #define 常量名 常量值
通常在文件上方定义,表示一个常量
2.const修饰的变量: const 数据类型 常量名=常量值
在变量定义前加个关键词const,修饰该变量为常量,不可修改
例:1. #define day7
int main(){
day=14; //错误,day是常量,一旦修改就会报错
cout <<"一周总共有:<<day <<”天"<<endl; //一周共有:7天
}
2. const 修饰的变量
int main (){
const int month =12;
month =24;//报错 const修饰的变量也称为常量
cout <<"一年有:"<<month<<"月"<<endl; //一年有12月
}
关键字(标识符)
•定义变量或常量时,不要用关键字
int main(){
//创建变量: 数据类型 变量名称=变量初始值
//不要用关键字给变量或者常量起名称 nint int=10(类型说明无效 int已经有别的含义了 再用做变量名编译器不干了)错误 第二个int是关键字,不可以作为变量的名称
int a =10;
system("pause");
return 0
}
标识名命名规则:标识符不可以是关键字 int int=10❌
标识符只能由字母 数字 下划线组成 int abc=10;✔️
第一个字符必须是字母或下划线 123abc=10❌
标识符区分大小写
建议:给变量起名时,最好做到见名识意,方便别人阅读
int main(){
system("pause")
return 0;
}