[previous] 4116 [next] Operand of logical ! operator is not an 'essentially Boolean' expression.
Arithmetic type - Operands REFERENCE - ISO:C90-6.3.3.3 Unary Arithmetic Operators

The operand of this logical negation operator ! is not an effectively Boolean expression.

The ISO;C90 language does not support an explicitly Boolean type. Type _Bool was introduced in ISO:C99 but is not recognised in the current version of QAC. In both versions of the language, there exist a number of operators which always yield a value of either 0 or 1 which is of type int. These operators are:

Although these operators yield a value of type int, the underlying type may be considered to be effectively.boolean.

The logical negation operator ! not only yields a result of effectively Boolean type, it also interprets its operand in a Boolean sense by comparing it with 0. According to the ISO:C standard, this operand can be of any scalar type. However, message 4116 is generated whenever the underlying type of the operand is NOT effectively Boolean.

So, for example, rather than using the ! operator (as in "!x"), it may be preferable to use the expression "x == 0", so that the logical comparison is made explicit and readable.

For example:


/*PRQA S 1278,2017,2213,3120,3198,3199,3203,3408,3447 ++*/

extern int flag;

extern void foo(void)
{
    if (!flag) {}               /* Message 4116 - operand is not Boolean        */
    if (!(flag != 0)) { }       /* Operand is Boolean, but expression is clumsy */
    if (flag == 0) { }          /* Explicit and readable                        */

    return;
}


No MISRA C:2012 Rules applicable to message 4116


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents