CH01_初识C++


第一个C++程序

新建项目

新建文件

编写代码

#include <iostream>
using namespace std;

int main() {
	cout << "hello world" << endl;
	system("pause");
	return 0;
}

运行程序

注释

作用:在代码中加一些说明和解释,方便阅读代码。

两种格式:

单行注释: //这是注释

多行注释: /这是注释/

#include <iostream>
using namespace std;

//入口函数
int main() {

	//控制台输出
	cout << "hello world" << endl;

	/*
	cout << "hello world" << endl;
	cout << "hello world" << endl;
	cout << "hello world" << endl;
	*/

	system("pause");
	return 0;
}

变量

作用:给一段指定的内存空间起名,方便操作这段内存。

语法:数据类型 变量名=初始值;

示例:

#include <iostream>
using namespace std;

//入口函数
int main() {

	//定义变量
	int num = 10;

	//控制台输出
	cout << "数字:" << num << endl;

	system("pause");
	return 0;
}

常量

作用:用于记录程序中不可更改的数据

语法:

1.#define 宏常量:#define 常量名 常量值

2.const修饰的变量:const 数据类型 常量名=常量值

示例:

#include <iostream>
using namespace std;

//宏常量
#define day 7

//入口函数
int main() {

	//常规常量
	const double pai = 3.14;

	//更改常量的值会报错
	//day = 8;
	//pai = 3.5

	//控制台输出
	cout << "每周天数:" << day << endl;
	cout << "圆周率:" << pai << endl;

	system("pause");
	return 0;
}

关键字

作用:关键字是C++中预先保留的单词(标识符)

提示:定义标识符时,禁止使用关键词,否则会报错。

示例:

#include <iostream>
using namespace std;


//入口函数
int main() {

	//标识符使用关键字会报错
	int int = 5;

	//控制台输出
	cout << "变量:" << int << endl;


	system("pause");
	return 0;
}

标识符

作用:C++规定给标识符(变量,常量等)命名时,有一套自己的规则

命名规则:

1.不能是关键字

2.只能由字母,数字,下划线组成

3.不能以数字开头

4.字母区分大小写

示例:

#include <iostream>
using namespace std;


//入口函数
int main() {

	//正确定义
	int num_1 = 5;	
	int a = 5;
	int A = 5;

	//错误定义
	int int = 5;
	int 3num = 5;
	int a&b = 5;

	system("pause");
	return 0;
}
posted on 2024-06-11 20:08  熊猫_大侠  阅读(4)  评论(0编辑  收藏  举报