-32768 on 16-bit targets

Moderators: misra-c, david ward

Post Reply
grunwald
Posts: 15
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

-32768 on 16-bit targets

Post by grunwald » Tue Feb 14, 2017 6:00 pm

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: Select all

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.

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: -32768 on 16-bit targets

Post by misra-c » Thu Mar 30, 2017 8:27 am

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.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”