6、编程入门:变量、运算与操作实践

编程入门:变量、运算与操作实践

1. 变量声明与使用

在编程中,变量的正确声明和使用至关重要。若尝试编译以下代码:

// Display some output
printf("%d brides for %d brothers\n", Brides, brothers);
return 0;
}

会得到错误信息。编译器会将 brides Brides 视为不同的名称,由于未声明 Brides ,编译器无法理解其指代内容。这是常见错误,标点和拼写错误是小错误的主要成因。使用变量前必须先声明,否则编译器无法识别,会将语句标记为错误。

以下是一个简单计算程序示例:

// Program 2.4 Simple calculations
#include <stdio.h>
int main(void)
{
  int total_pets;
  int cats;
  int dogs;
  int ponies;
  int others;
  // Set the number of each kind of pet
  cats = 2;
  dogs = 1;
  ponies = 1;
  others = 46;
  // Calculate the total number of pets
  total_pets = cats + dogs + ponies + others;
  printf("We h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值