MISRA Discussion Forums
-32768 on 16-bit targets - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165)
+---- Thread: -32768 on 16-bit targets (/showthread.php?tid=1311)



-32768 on 16-bit targets - grunwald - 14-02-2017

Just to make sure since several of our customers brought this up lately:

Is the following code compliant on a target where 'int' is 16 bits?

Code:
int16_t x = -32768;

Since the standard C type of '-32768' is 'long' on 16-bit targets, the essential type is the same.
Thus, I believe the code is not compliant.

This seems unfortunate given that the value does fit in the target type; especially since the same code is compliant on targets with 32-bit int.


Re: -32768 on 16-bit targets - misra-c - 30-03-2017

You are correct to say that the code is not-compliant.

The C standard type is "signed long", since it is the result of a unary negation on a literal whose type is "signed long".

The essential type and C standard type of an expression may only differ if the C standard type is "int" or "unsigned int". This is not the situation, so the essential type is also "signed long".

Using INT_MIN from the limits.h will return an expression with the correct value and "signed int" type.