1

In Java I can do:

int i = 1_200_200;

How can I do something the same in c++? I mean what should I use instead of an underscore?

2 Answers2

4

Since C++14 you can use single quotes (') for integer literal to improve the readability, e.g.

int i = 1'200'200;

Optional single quotes(') may be inserted between the digits as a separator. They are ignored by the compiler.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • @IharobAlAsimi Huh? For the same reason it's used in natural language of course, i.e. readability. For binary constants it would be very useful I think, i.e. `0xde'ad'f0'0d` and so on. – unwind Sep 16 '17 at 12:53
0

In C++ you can use the ordinary quote. For example

#include <iostream>

int main() 
{
    int x = 1'234'567;

    std::cout << "x = " << x << std::endl;

    return 0;
}

In C such a feature is absent.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335