简体   繁体   中英

MISRA-C: cast literal number

I read a source code, there is a statement like: uint32 XYZ; ... XYZ = (uint32)0x0000000U.

I wonder if the cast is necessary, is XYZ = 0U OK?

Thanks

The integer constant 0U can in theory be an unsigned int of 64 bits. If so, the cast is necessary since MISRA-C (2012 10.3) does not allow implicit conversion to a narrower type. However, the cast isn't necessary on 32 bit CPUs and smaller.

An alternative is to write uint32_t XYZ = UINT32_C(0); in which case the compiler picks the appropriate type for the integer constant automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM