c - 左值需要作为 c 中赋值错误的左操作数
问题描述
我必须编写一个比较 3 个整数的程序。我不明白为什么我不能将变量 a 分配给 min 或 max 变量。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, max, notmax;
printf("enter first integer\n");
scanf("%d", &a);
printf("enter second integer\n");
scanf("%d", &b);
printf("enter third integer\n");
scanf("%d", &c);
a > b ? a = max : a = notmax ;
return 0;
}
解决方案
查看优先级和关联性可能会帮助您了解这里发生的事情。赋值的优先级低于 ?: 运算符。所以声明
a > b ? a = max : a = notmax ;
被视为:
((a > b ? a = max : a) = notmax );
但是一旦您在适当的位置使用括号,如下所示,一切正常:
a > b ? a = max : (a = notmax) ;
或者甚至可能是这样的:
(a > b? (a = max) : (a = notmax)) ;
这应该按照您想要的方式强制优先。使用方括号将有助于评估复合语句。
推荐阅读
- java - 这个非常简单的猜谜游戏行不通
- php - 使用服务帐户在网站上显示 Google Drive 视频
- django - 如何将 POST 数据中的 CSRF 令牌传递给 Django?
- javascript - 如何让 Discord Bot 确保您在命令中 ping 某人
- python - SyntaxError:无效的语法 - python 字典属性
- java - 将 azure Application Insights SDK 与 quarkus jar Web 应用程序一起使用?
- shell - 在 awk 中读取多个文件并将每个文件分别打印在另一个文件下面
- python - 在使用新元素更新字典时,是否有一种使用计数器作为键的 Pythonic 方法?
- excel - VBA Excel 到 Word - 另存为 pdf 在第二次循环运行时失败
- javascript - Promise 应该在结束两个 for 循环后返回数据