05-01-2006, 11:12 AM
ANSWER: MISRA-C Steering Team
See section 6.10 in MISRA-C:2004.
1. The expression
u16a = u16b + u16c;
Underlying type is used to address this issue.
While the expression u16b + u16c gives a 32-bit result on a 32-bit machine, the underlying type of the expression is U16 regardless of machine architecture.
2.
int16_t foo1(void)
{
return(20000); /* compliant on the 16 bit machine */
return(20000); /* compliant on the 32 bit machine */
return(20000L); /* not compliant on any machine */
return(20000U); /* not compliant on any machine */
}
20000 can be represented in 16 bits, with underlying type of signed 16 bits.
20000L is explicitly long, and at least 32 bits wide, with underlying type of signed long which could be S32, S64 or ...
20000U is explicitly unsigned, with underlying type of unsigned 16 bits. Its use in the return expression involves a change of signedness.
Generally, avoid the use of the long suffix L on integer constants.
See section 6.10 in MISRA-C:2004.
1. The expression
u16a = u16b + u16c;
Underlying type is used to address this issue.
While the expression u16b + u16c gives a 32-bit result on a 32-bit machine, the underlying type of the expression is U16 regardless of machine architecture.
2.
int16_t foo1(void)
{
return(20000); /* compliant on the 16 bit machine */
return(20000); /* compliant on the 32 bit machine */
return(20000L); /* not compliant on any machine */
return(20000U); /* not compliant on any machine */
}
20000 can be represented in 16 bits, with underlying type of signed 16 bits.
20000L is explicitly long, and at least 32 bits wide, with underlying type of signed long which could be S32, S64 or ...
20000U is explicitly unsigned, with underlying type of unsigned 16 bits. Its use in the return expression involves a change of signedness.
Generally, avoid the use of the long suffix L on integer constants.