重新源$ C $ C以增强可读性presenting大的数字? [英] Representing big numbers in source code for readability?
问题描述
是否有书面的C ++或C?
Is there a more human-readable way for representing big numbers in the source code of an application written in C++ or C?
让我们比如拿号 2,345,879,444,641
,C或C ++如果我们想要一个程序来恢复这个数字,我们会做返回2345879444641
。
let's for example take the number 2,345,879,444,641
, in C or C++ if we wanted a program to return this number we would do return 2345879444641
.
但是,这是不是真的读。
But this is not really readable.
在PAWN(一种脚本语言)比如我可以做收益2_345_879_444_641
甚至收益2_34_58_79_44_46_41
键,这些都将返回数 2,345,879,444,641
。
In PAWN (a scripting language) for example I can do return 2_345_879_444_641
or even return 2_34_58_79_44_46_41
and these both would return the number 2,345,879,444,641
.
这是对人眼更具有可读性。
This is much more readable for the human-eye.
是否有一个C或C ++相当于呢?
Is there a C or C++ equivalent for this?
推荐答案
下面是一个宏,将做到这一点,在两个MSVC和GCC测试。在升压不依赖...
Here's a macro that would do it, tested on both MSVC and GCC. No reliance on Boost...
#define NUM(...) NUM_(__VA_ARGS__, , , , , , , , , , )
#define NUM_(...) NUM_MSVCHACK((__VA_ARGS__))
#define NUM_MSVCHACK(numlist_) NUM__ numlist_
#define NUM__(a1_, a2_, a3_, a4_, a5_, a6_, a7_, a8_, ...) a1_##a2_##a3_##a4_##a5_##a6_##a7_##a8_
使用它像:
int y = NUM(1,2,3,4,5,6,7,8);
int x = NUM(100,460,694);
产地:
int y = 12345678;
int x = 100460694;
这篇关于重新源$ C $ C以增强可读性presenting大的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!