打卡第二天VS2019 C++初学者关于变量定义

1.宏常量

#define 宏常量

#define 常量名  常量值      例: #define  Day  7        代表Day的值为7

例如程序

#include <iostream>
using namespace std;

#define Day 7   
int main()
{

    cout << "一周有:" << Day << "天"<< endl;
    system("pause");

    return 0;
}
输出结果为

2.const 修饰的变量  

const 数据类型 常量 = 常量值

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

例如 int a =10; 常量值可修改,而在其前面添加const  int a = 10 ;此时则不能修改,变量值变成常量值

例如程序

#include <iostream>
using namespace std;

#define Day 7   
int main()
{

     const int month = 12;
     //month = 24;//错误,const 修饰的变量也称为常量
     cout << "一年有:" << month << "月" << endl;


    system("pause");

    return 0;

}

屏幕显示结果为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值