首页 > 解决方案 > 在这个简单的 C 程序中无法理解这个错误

问题描述

我不知道为什么当我执行这个程序时,它一直告诉我代码“salary = 40 * RATE + (hours-40) * OVERTIME_RATE;” 在 if 语句中是“未使用的表达式结果”。请告诉我它有什么问题。非常感谢你。

#include <stdio.h>
#define RATE 15.0;
#define OVERTIME_RATE 25.0;

int main(void)
{
  int emp_no;
  double hours, salary;

  printf("Employee Number: ");
  scanf("%d", &emp_no);
  printf("Enter the hours worked this week: ");
  scanf("%lf", &hours);

  if (hours <= 40.0){
     salary = hours*RATE;
  }else {salary = 40 * RATE + (hours-40) * OVERTIME_RATE;}

  printf("Pay of Employee %d is S$%8.2f.\n", emp_no, salary);

  return 0;
}

标签: c

解决方案


这是错误的

#define RATE 15.0;

预处理器通过文本替换工作,所以每次你提到RATE, 都被替换为15.0;,但你希望它被替换为15.0.

因此你应该写:

#define RATE 15.0

当然,这同样适用OVERTIME_RATE

用您的宏定义替换的结果:

{salary = 40 * 15.0; + (hours-40) * 25.0;;}

这与

{
  salary = 40 * 15.0;
  + (hours-40) * 25.0;;    // this line contains just an expression
                           // that is evaluated but not used, hence the warning
}

推荐阅读