在C源中使大常量更易读? [英] Making large constants in C source more readable?
问题描述
我正在研究微处理器的一些代码。
它有一些很大的关键常数。
#define F_CPU 16000000UL
在这种情况下,这是CPU频率。在Hertz。
因此,很难判断是1600万,160,000,000或16,000,000,而无需手动将光标移动到数字上。
如果我在数字 #define F_CPU 16,000,000UL
中输入逗号,则会截断该常量。
$ b $我已经使用了一些具有特定数字分隔符的深奥语言,旨在使大量数据更易读(ex
16_000_000
,主要是以语言为准对于MCU)。大量的魔术数字在嵌入式的东西中是很常见的,因为它们需要描述MCU如何与现实世界进行对话的方面。 有没有这样的C?
一种可能性是这样写:
#define F_CPU(16 * 1000 * 1000)
#define MHz(1000 * 1000)
#define F_CPU(16 * MHz)
编辑:其他建议的MHz(x)可能更好
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 CPU frequency. In Hertz.
As it is, it's rather hard to tell if that's 1,600,000, 160,000,000 or 16,000,000 without manually tabbing a cursor across the digits.
If I put commas in the number #define F_CPU 16,000,000UL
, it truncates the constant.
I've worked with a few esoteric languages that have a specific digit-separator character, intended to make large numbers more readable (ex 16_000_000
, mostly in languages intended for MCUs). Large "magic numbers" are rather common in embedded stuff, as they are needed to describe aspects of how a MCU talks to the real world.
Is there anything like this in C?
One possibility is to write it like that:
#define F_CPU (16 * 1000 * 1000)
alternatively
#define MHz (1000*1000)
#define F_CPU (16 * MHz)
Edit: The MHz(x) others suggested might be nicer
这篇关于在C源中使大常量更易读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!