21

Java allows the digits in a numeric constant to be separated as follows:

int a = 1_000_000;

Does C or C++ have a similar construct?

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
Igorzovisk
  • 634
  • 6
  • 19

2 Answers2

25

The only way to do this is in C++14, is with single quotes, like this. Unfortunately, the only problem with this is that syntax highlighting often gets messed up with the notation below, and you can see that in my example as well:

int i = 1'000'000;


Working Example
According to http://en.cppreference.com/w/cpp/language/user_literal:

In the integer and floating-point digit sequences, optional separators ' are allowed between any two digits and are ignored (since C++14)

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
19

You may write in C++ 14

int a = 1'000'000;

In C such a feature is absent.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335