Making large constants in C source more readable?

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

    It might help readability to define the constant as:

    #define F_CPU_HZ 16000000UL
    

    That way you know what type of data is in it. In our SW we have a few peripherals which require assorted prescalers to be set, so we have a #defines like this:

    #define SYS_CLK_MHZ    (48)
    #define SYS_CLK_KHZ    (SYS_CLK_MHZ * 1000)
    #define SYS_CLK_HZ     (SYS_CLK_KHZ * 1000)
    

提交回复
热议问题