|
|||||||||||
Technical Support Support Resources
Product Information |
GENERAL: #DEFINE Generates Wrong ResultsInformation in this article applies to:
QUESTIONI have found a serious problem when I use simple constants in a #define statement. In the following code the constant is not divided:
The value will not be (MAX_MSG_LEN/4) it is just MAX_MSG_LEN. MAX_MSG_LEN is defined as follows:
ANSWERThe #define is a pure text replacement utility. So you have effectively written if (value >= (16+3/4)). The ANSI C compiler calculations plain numbers with the arithmetic rules for int variables. Therefore, 3/4 is calculated first which results in 0 and the result of the complete calculation is just 16. This lets you believe that the compiler does not divide. As a general rule, it is recommended that you use parenthesis in #define statements. For example:
This avoids simple programming mistakes as you had above. MORE INFORMATION
SEE ALSO
Last Reviewed: Thursday, February 25, 2021 | ||||||||||
|
Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.