Making large constants in C source more readable?

后端 未结 8 836
遇见更好的自我
遇见更好的自我 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:15

    maybe something like that?

    #define MHz(x) (1000000 * (x))
    ...
    #define F_CPU MHz(16)
    

    Also, I don't like #defines. Usually it's better to have enums or constants:

    static const long MHz = 1000*1000;
    static const long F_CPU = 16 * MHz;
    

提交回复
热议问题