[previous] 3611 [next] Non-portable comparison of plain 'char' with negative constant.
Arithmetic type - Operations REFERENCE - ISO:C90-6.1.2.5 Types

An operand of type 'char' is being compared with a negative value using a relational or equality operator.

In the C language there exist 3 distinct char types:

It is recommended that signed char and unsigned char should be used for "numeric" data and plain char should be used for "character" data. Numeric data can be stored in type char but the range of values will be implementation-defined because the type is sometimes implemented as a signed type and sometimes as an unsigned type. Likewise, character data can be stored in type signed char or type unsigned char, but it is more sensible to use type char.

If the value of an operand of type 'char' is compared with a negative value, an implicit assumption is being made that 'char' is a signed type and the operation is intrinsically implementation defined.

For example:


/*PRQA S 2106,2213,3120,3227,3408,3625,3447 ++*/

extern char ch;

extern void foo(void)
{
    if (ch == -40)  { }                /* Message 3611 */
    if (ch != -40)  { }                /* Message 3611 */
    if (ch >= -10)  { }                /* Message 3611 */
    if (ch >  -10)  { }                /* Message 3611 */
    if (ch <= -10)  { }                /* Message 3611 */
    if (ch <  -10)  { }                /* Message 3611 */

    if (ch <  0)    { }                /* Message 3612 */
    if (0 >  ch)    { }                /* Message 3612 */
}


No MISRA C:2012 Rules applicable to message 3611


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