why adding #define contsants for initializing a const is breaking rule 10.3 ?

Moderators: misra-c, david ward

alaini
Posts: 2
Joined: Thu Sep 13, 2018 5:34 pm
Company: STM

why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby alaini » Fri Sep 14, 2018 8:28 am


GerlindeKettl
Posts: 9
Joined: Thu Apr 15, 2010 8:01 am
Company: Continental AG

Re: why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby GerlindeKettl » Mon Sep 17, 2018 8:20 am

According to C99, a decimal constant with suffix U is of type unsigned int. (CONST1 + CONST2) is then an expression of type unsigned int. If unsigned int has a size of 32 bit in your environment, an expression with a size of 32 bit is assigned to a variable of type uint16_t which is a narrower essential type.

alaini
Posts: 2
Joined: Thu Sep 13, 2018 5:34 pm
Company: STM

Re: why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby alaini » Mon Sep 17, 2018 10:24 am

Thanks for the answer.
There is an exception to this rule for the direct assignment of decimal constant with suffix U, why the exception is not extended when this is the addition of such constant which results in the end in exactly the same type?

GerlindeKettl
Posts: 9
Joined: Thu Apr 15, 2010 8:01 am
Company: Continental AG

Re: why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby GerlindeKettl » Wed Sep 19, 2018 11:48 am

Which exception do you mean? Exception 1 states that a non-negative integer constant expression of essentially signed type may be assigned to an object of essentially unsigned type if its value can be represented in that type. But I don't see an exception about decimal constants with suffix U.

Francois
Posts: 9
Joined: Thu Jul 13, 2017 2:22 pm
Company: TE CONNECTIVITY

Re: why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby Francois » Thu Sep 20, 2018 6:33 am


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

Re: why adding #define contsants for initializing a const is breaking rule 10.3 ?

Postby misra-c » Thu Oct 04, 2018 10:58 am

---
Posted by and on behalf of
the MISRA C Working Group


Return to “8.10 The essential type model”

Who is online

Users browsing this forum: No registered users and 1 guest