c - 在这个简单的 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;
}
解决方案
这是错误的
#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
}
推荐阅读
- gitlab-ci - 如何在 gitlab-ci 脚本中执行 git 命令
- javascript - 相机 LED 灯未在 JavaScript Web 应用程序中运行
- python - _csv.Error:迭代器应该返回字符串,而不是 int(您是否以文本模式打开文件?) sqlalchemy
- java - 在开关中使用滑行(在 DataFakeGenerator 类中)
- python - 根据第二个更新数据框中的行
- php - 我有什么地方可以添加刷新标题而不会导致循环?
- python-2.7 - 如何迭代目标函数
- linux - 性能:无法跟踪 tp->snd_cwnd_clamp
- tensorflow - 插件的数据输入格式
- python - 如何在reportlab python中将文本对齐到中心?