[C语言]变量VS常量

--------------------------------------------------------------------------------------------

1. 固定不变的数是常数,直接写在程序里称为直接量(literal)。

 

  int total = 100 - price;

 

#include <stdio.sh>

int main()
{
  int price = 0;       //初始化

  printf("请输入金额:");
  scanf("%d", &price);     //注意别忘了取地址符

  int change = 100 - price;

  printf("找您%d元", change);    
}

 

2. 回过头来我们并不知道上面的100是什么数,为了方便我们辨识,这里使用一个常量。

 

  const修饰符加在int前面用来表示常量,用const定义的属性,一旦初始化,不能被修改(PHP5也是如此); 如果对常量进行修改赋值,编译器将报错(Read-only variable is not assignable),对常量约定使用大写。

   

  const int AMOUNT = 100;

  int change = AMOUNT - price;

 

const int AMOUNT = 100;
    
int price = 0;
        
printf("请输入金额:");     
scanf("%d", &price);
        
int change = AMOUNT - price;
        
printf("找您%d元", change); 

 

3. 现在不想定义常量,那么使用多个scanf

  

int amount;  //未初始化

int price;

/*
printf("请输入两个整数:");
scanf("%d %d", &amount, &price); //两个数以空格或回车隔开;注意:遇到非数值输入 如字符串时,scanf将出错,内存里有什么就取什么
printf("%d + %d = %d", amount, price, amount + price);
*/
prinf(
"请输入票面:"); scanf("%d", &amount); printf("请输入金额:"); scanf("%d", &price); int change = amount - price;
if(change > 0) {
  printf(
"找您%d元", change); 
} else {
  printf("您的钱不够!\n");
}

 

Link:http://www.cnblogs.com/farwish/p/4167669.html

@黑眼诗人 <www.Farwish.com> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值