Making large constants in C source more readable?

后端 未结 8 840
遇见更好的自我
遇见更好的自我 2020-12-19 06:34

I\'m working on some code for a microprocessor.
It has a few large, critical constants.

#define F_CPU 16000000UL

In this case, this is the C

相关标签:
8条回答
  • 2020-12-19 07:21

    You could write the constant as the result of a calculation (16*1000*1000 for your example). Even better, you could define another macro, MHZ(x), and define your constant as MHZ(16), which would make the code a little bit more self-documenting - at the expense of creating name-space collision probability.

    0 讨论(0)
  • 2020-12-19 07:22

    You can use scientific notation:

    #define F_CPU 1.6e+007
    

    Or:

    #define K 1000
    
    #define F_CPU (1.6*K*K)
    
    0 讨论(0)
提交回复
热议问题