初识c++变量常量关键字

1初识c++

1.1变量

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

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

实例:

#include <iostream>
using namespace std;

int main() {

	数据类型 变量名 = 初始值;
	int sum = 10;
	cout << "sum=" << sum << endl;

	system("pause");

	return 0;
}

1.2常量

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

c++定义常量两种方式

#define宏定义: #define 常量名 常量值

  • 通常在文件上方定义,表示一个常量

const修饰的常量 const 数据类型 常量名 = 常量值

  • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改

实例:

#include <iostream>
using namespace std;
//常量的定义
//1、#define 宏常量
//2、const修饰的变量

//1、#define 宏常量
#define Day 7

int main() {


	cout << "一周有" << Day << "天" << endl;

	//2、const修饰的变量
	const int month = 12;
	cout << "一年有" << month << "个月份" << endl;
	system("pause");

	return 0;
}

1.3关键字

autobreakboolcasecharcatchclassconst
continuedefaultdeletedodoubleelseenumexplicit
exportexternfalsefloatforfriendgotoif
inlineintlongmutablenewnamespaceoperatorprivate
portectedpublicregisterreturnshortsignedsizeofstatic
static_caststructswitchthistruetypedeftypenameunion
unsignedusingvirtualvoidvolatilewhile
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值