3

Many languages allow you to insert underscores into long hex numbers to improve readability. I would like to put underscores into hex numbers that define forty bit hardware addresses. For example I would like to write 0x0400000000 as 0x04_0000_0000.

It is tough for me to count consecutive zeros but I can handle groups of four.

Does C or C++ allow punctuation inside hex constants?

Is there a practical workaround if something like this is not allowed in the language?

Thanks,

Pedro_Uno
  • 946
  • 10
  • 19
  • There's no such support for C. So that's that. For the rest: it's a duplicate question. – Kuba hasn't forgotten Monica Mar 30 '19 at 20:19
  • 4
    TL;DR: C++14 allows punctuation inside any integer literals, and the punctuator is `'`. So, simply write `0x04'0000'0000` in a modern compiler. For C: your best bet is to either write C that's valid C++ and use a modern C++ compiler to compile it in C++ mode, or write a separate preprocessor to dump the punctuation from integer literals, or modify gcc/clang to accept such punctuation (it's a very minor change - a few lines at most). – Kuba hasn't forgotten Monica Mar 30 '19 at 20:24
  • @KubaOber That's worth an answer because it's really useful ! – Christophe Mar 30 '19 at 20:28
  • Thanks guys, my question was largely duplicate but the advice is very useful in any case. I think I will try to use a C++ compiler on my code, just to allow such punctuation. Also, thanks to the compiler writers that added this feature. – Pedro_Uno Mar 30 '19 at 20:51
  • 1
    @KubaOber: are you seriously suggesting that the OP produce a non-standard version of the C compiler just to solve this minor problem? That is madness! – TonyK Mar 30 '19 at 21:42
  • Open source makes such minor changes trivial. Why not leverage it?? – Kuba hasn't forgotten Monica Mar 30 '19 at 23:55

0 Answers0