码迷,mamicode.com
首页 > 其他好文 > 详细

为什么需要常量

时间:2018-08-02 12:18:47      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:name   mes   names   getc   turn   col   ret   ogr   是什么   

为什么需要常量

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

(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 }

 

为什么需要常量

标签:name   mes   names   getc   turn   col   ret   ogr   是什么   

原文地址:https://www.cnblogs.com/borter/p/9406270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!