const与define的相同之处:定以后的变量是不允许在后续的程序代码中修改的,定义变量代替所定义的变量或数值
const与define的不同之处:const是有作用域的,但是define是全局的
#include "iostream"
using namespace std;
//const与define的相同之处,可以对变量设置定值
#define b 20
void main05()
{
const int a = 10;//在后续的程序中a是不允许修改的
char array[a + b];//经过const与define定义的变量可以用来作为数组的计算
system("pause");
}
//const与define的不同之处,const是有作用域的,但是define是全局的
void function1()
{
#define x1 20.5;
const int x2 = 20;//只能在function1函数中使用,因为const有作用域限制
//#undef x1;卸载x1的全局作用域
//#undef ;卸载define的所有全局变量
}
void function2()
{
cout << "x1=" << x1;
//cout << "x2=" << x2;x2只能在function1中使用
}
void main06()
{
function1();
function2();
system("pause");
}