Suggest parentheses around '&&' within '||' [-Werror=parentheses]

Hi everyone.
The line got error of: suggest parentheses around '&&' within '||' [-Werror=parentheses]

if(batteryVoltage < 3.78 && batteryVoltage || antiFlicker == 3 )

modified as below eliminated the error.

if(((batteryVoltage < 3.78) && batteryVoltage) || antiFlicker == 3 )

Will the modification effect the correctness of the sketch?
Thanks
Adam

The main reason you would do it is for absolute clarity about the order of the operations that you want.

Without the parentheses you need to understand the order of precedence that each of the operations has.

https://en.cppreference.com/w/cpp/language/operator_precedence

2 Likes

Great!
Thank you.

You ar comparing batteryVoltage to a float and then treat it as a bool.
In that second occurrence, any other value than 0.00000 will evaluate to true.
It is not good practice to compare a float to an exact number.
Consider batteryVoltage > 0.01.

1 Like

It is not an error but warning that is turned into error with -Werror flag. Makes sense if you are a big company and don’t want someone commit the code that has warnings, are you a big company?

1 Like

What difference does that make? It's always a best practice to eliminate all warnings.

1 Like

Does one have to prevent code compiling in order to get rid of warnings?

1 Like

Write the code so it compiles without warnings.

1 Like

You haven’t answered my question, I’ll answer it for you - NO.

1 Like

Question made no sense, so I didn't bother to address it.

1 Like

Thanks.

thanks.
not company.
how to remove -Werror flag?

what platform/software are you compiling on

1 Like

Because warnings are informative, errors are blockers. making warnings blockers is authoritarian unless you did it to yourself. The whole philosophy of C++ is let the user screw up on their own accord and herein lies freedom. If one wants holding hands language they can switch

1 Like

Thanks for the expanded explanation of your point. I generally agree.

My initial read was that you were advocating not fixing code to eliminate warnings.

1 Like

Arduino IDE 1.8.19
thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.