1、第一个程序
-
创建新项目
-
创建文件
-
书写代码
#include <iostream> using namespace std; //1.单行注释 //2.多行注释 /* main 是一个程序的入口 每个程序都必须有这么·一个函数 */ int main() { //11行在屏幕中输出hello world cout<<"hello world"<<endl; system("pause"); return 0; }
运行结果
1.2注释
1. 单行注释//
2.多行注释/*内容 */
1.3 变量(意义:给指定的空间取名,方便我们管理内存空间)
变量创建的语法: 数据类型 变量名=变量初始值
int a= 10;
#include<iostream> using namespace std; int main() { //变量创建的语法: 数据类型 变量名=变量初始值 int a=10; cout << "a="<<a<<endl; system("pause"); return 0; }
运行结果如图:
1.4 常量(记录程序中补课修改的数据)
C++定义常量的两种方式:
1.#define 宏常量
2.const修饰的变量 const
#include <iostream> using namespace std; //常量的定义方式 //1.#define 宏常量(文件的上方) #define Day 7 //2.const 修饰的变量 int main() { //Day = 14;//错误,Day是常量,一旦修改就会报错 cout<<"一周有:"<<Day<<"天"<<endl; //2.const 修饰的变量(文件里面) const int month = 12; //month = 24; //错误,const 修饰的变量也成为常量 cout<<"一年有:"<<month<<"月"<<endl; system("pause"); return 0; }
运行结果如图:
1.5 关键字(C++中预先保留的单词)
在定义变量或常量时候,不建议使用
#include<iostream> using namespace std; int main() { //变量创建的语法: 数据类型 变量名=变量初始值 //不用关键字给变量或常量起名 //int int = 10 ; false int a=10; cout << "a="<<a<<endl; system("pause"); return 0; }
运行结果如图:
1.6 标识符命名规则
作用:C++规定给标识符(常量、变量)命名时,有自己的规则
-
标识符不能是关键字
-
标识符只能由字母、数字、下划线组成
-
第一个字符必须为字母或下划线
-
标识符中字母区分大小写
#include<iostream> using namespace std; int main() { //* 标识符不能是关键字 //int int = 10; //* 标识符只能由字母、数字、下划线组成 int abc=10; //* 第一个字符必须为字母或下划线 int _abc=20; int _123abc=30; //* 标识符中字母区分大小写 int aaa=100; //cout << AAA<<endl;///aaa和AAA不是同一个名 //见名字知意思 system("pause"); return 0; }