![](/img/trans.png)
[英]warning: suggest parentheses around assignment used as truth value [-Wparentheses]
[英]Keep getting warning "Suggest parentheses around '&&' within '||' for C program
printf(" 1| %d | %d | %d | %d | %d\n",
((coffee_strength == 'm' || coffee_strength == 'M') &&
(coffee1_type == 'l' || coffee1_type == 'L') ||
(coffee_strength == 'r' || coffee_strength == 'R') &&
(coffee1_type == 'b' || coffee1_type == 'B')),
((coffee_maker == 'r' || coffee_maker == 'R') &&
(coffee1_grindSize == 'c' || coffee1_grindSize == 'C') ||
(coffee_maker == 'C' || coffee_maker == 'c') &&
(coffee1_grindSize == 'f' || coffee1_grindSize == 'F')),
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) ||
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000))),
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
這是我正在嘗試編譯的代碼。 每次我運行它時,我仍然會遇到與支架放置相關的問題。 我已經多次編輯我的代碼來嘗試解決這個問題,但問題仍然存在。 有人對我應該做什么有任何建議嗎?
很抱歉,但這似乎是通過觀察空間來判斷一切。 有數百萬顆恆星、行星、小行星、彗星、流星。
但我非常感謝您在格式化方面所做的努力。 不是開玩笑。
問題似乎在這里:
&& (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
讓我們擴展它:
&& (coffee1_temp <= 69.9) && (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
我不確定您想在該行中實現什么,但這里有一個示例可以解決該行中的錯誤:
&& (coffee1_temp <= 69.9) && ( (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')) ) ));
為什么會發生錯誤? 你有a || c && d
a || c && d
/ a && b || c && d
a && b || c && d
。 編譯器會警告您,因為您沒有告訴它在這種情況下要做什么。
示例: a && c || c && d
a && c || c && d
,首先應該發生什么? c || c
c || c
或a && c
或c || c && d
c || c && d
等等......非常混亂......
似乎您在許多其他地方也有這個。 您可以只使用單獨的 if 語句來使這一點更清晰......畢竟閱讀它似乎需要更多時間而不是編寫一組新的 if 語句。
我正在努力使它更具可讀性,請再給我幾分鍾。
希望更具可讀性:
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
|| (coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
),
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
|| (coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
),
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
),
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
|| (coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
),
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
現在,真的很難弄清楚問題是什么嗎?
修復,可能不符合您的預期:
(
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
)
|| (
(coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
)
)
(
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
)
|| (
(coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
)
)
(
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
)
(
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
)
|| (
(coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
)
)
(
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
)
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
正如@user3386109 所說,您可以簡化檢查大寫和小寫的語句,例如a == 'A' || a == 'a'
a == 'A' || a == 'a'
to toupper(a) == 'A'
使用<ctype.h>
中的toupper(int c)
和tolower(int c)
運算符&&
的優先級高於運算符||
. 因此,雖然合法,但編譯器會告訴您執行此操作的代碼:
a && b || c && d
可能沒有按照您的意願行事。 編譯器會將其視為
(a && b) || (c && d)
但據它所知,您打算做許多其他事情中的任何一件。
也許是這樣:
a && (b || (c && d))
或這個:
((a && b) || c) && d
或者也許......這個:
a && (b || c) && d
等等
它有所作為,這樣的錯誤很常見,所以雖然你的代碼會編譯,但當適當地要求這樣做時,它會發出警告(而且總是適當的要求)。 不是因為它是錯誤的,而是因為你沒有足夠清楚地表明警告猴子被制服(在這種情況下,大多數情況下,這是一件好事)為了確保你得到你所期望的,編譯器正在詢問你通過括號澄清你的表達。 如果您使用的是 gcc 或 clang,則包含在 -Wall 中的 -Wlogical-op-括號很可能會告訴您這一點。
從表面上看,您已經承擔了嘗試解決此問題的職責,但是您錯過了幾個實例。 例如:
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) || // <== here
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000)))
下面還有其他的,我留給你找(一旦你開始嵌套你的括號並意識到有多少地方a && b || c
彈出)。
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
和這里:
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') || // <== here
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.