[previous] 2940 [next] Constant: Result of implicit conversion is only representable in a two's complement implementation.
Conversion to signed

A negative value is being implicitly converted to a signed type and the value is such that it can only be represented in a signed type of this size in a two's-complement form. In practice, two's complement representation is almost universal in modern computers, but other binary representations do exist.

Message 2940 will be generated when:

Notice that on a 16 bit architecture:
  1. 32767 is a constant of type signed int
  2. 32768 is a constant of type signed long
  3. -32768 is an expression of type signed long

The expression "-32767 - 1" is of type signed int - BUT its evaluation will result in overflow if the signed representation is not two's complement.

Example:


/*PRQA S 274,280,1278,2017,2982,2984,3120,3198,3210,3408,3447,3602,3606 ++*/
/***************************************************/
/* OPTIONS: -s int=16                              */
/***************************************************/

extern signed char  sca;
extern signed short ssa;
extern signed int   sia;

extern void foo(void)
{
    sca = -127;
    sca = -128;                         /* 2940 */
    sca = -129;                         /* 2850 */

    ssa = -32767;
    ssa = -32767 - 1;                   /* 2940 */
    ssa = -32769L;                      /* 2850 */

    sia = -32767;
    sia = -32767 - 1;                   /* 2940 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents