为什么需要常量

为什么需要常量

如果不使用常量,直接在程序中填写数字或字符串,将会有什么麻烦?

(1) 程序的可读性(可理解性)变差。程序员自己会忘记那些数字或字符串是什么意 思,用户则更加不知它们从何处来、表示什么。

(2) 在程序的很多地方输入同样的数字或字符串,难保不发生书写错误。

(3) 如果要修改数字或字符串,则会在很多地方改动,既麻烦又容易出错

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     
 7     int a,b,Max;
 8     //输入数据
 9     cout<<"a=";
10     cin>>a;
11     cout<<"b=";
12     cin>>b;
13 
14     //找出较大值
15     Max=a>b?a:b;
16     cout<<"Max="<<Max<<endl;
17     return 0;
18 }
原文地址:https://www.cnblogs.com/borter/p/9406270.html