首页 > 解决方案 > 为什么 C 中的条件运算符在我的情况下不起作用?

问题描述

我有这段代码

 int8_t startPage = ( ((uint8_t)(ceilf( (float)CurrentY / 8))) - 1);
 /* variable = condition ? value_if_true : value_if_false*/
 startPage<0 ? 0:startPage;

如果CurrentY为 NULL,startPage则为 -1。但是页面不能为负数。

所以我尝试检查它,如果 startPage 为负,则将其设置为 NULL。

但我有一个警告statement with no effect [-Wunused-value]

并且代码不起作用。有任何想法吗?

标签: c

解决方案


startPage<0 ? 0:startPage;未使用from 的值,因此编译器会警告您。

您可以执行以下操作:

startPage = (startPage < 0) ? 0 : startPage;

如果小于,则设置startPage为。00


推荐阅读