oPolo
oPolo

Reputation: 576

Digit separator for literals in C

I have been able to use the digit-separator ' in my C projects thus far. They have all been compiled with the MSVC compiler with no problems.

I have just changed to using the GCC compiler instead, which does not allow these digit-separators and throws an error, when I use them. I assume the reason is because, Visual Studio uses the same compiler for C and C++, and in C++14 (I believe), ' digit separators are allowed.

If this is the case, is there then a digit separator usable in GCC? Those separators really help out a lot, when working with 64 bit register values in binary...

Upvotes: 8

Views: 6408

Answers (2)

poly000
poly000

Reputation: 131

It's defined in C23 n2626, thus, you can use this feature directly with --std=c23 (for gcc 14.x or newer, or clang 16.x or newer).

Or --std=c2x in older compilers (gcc 12.x or newer, or clang 13.x or newer).

The reason you can compile it with MSVC is, MSVC is not a true C compiler, rather, it inteprets your C program as C++. Since C++14 supports digit-separator ' as digit separator, it compiles.

Upvotes: 13

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27516

You should use -std=c++1y gcc/g++ option (and have the file named like a C++ file, e.g. cpp/cxx extension) to use ' in number literals.

Upvotes: -4

Related Questions