4

What exactly as been assigned to value? I am surprised that this has compiled at all.

//g++  7.4.0

#include <iostream>

int main()
{
    auto value = 123'456'7;
    std::cout << value << std::endl;

    value += 1;
    std::cout << value << std::endl;
}

outputs:

1234567
1234568
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • you can use ' to separate thousands – Raildex Nov 17 '20 at 11:18
  • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3781.pdf the feature was added in cpp 14 – Lesiak Nov 17 '20 at 11:19
  • 2
    @Raildex: convention of separation depend on country, C++ doesn't enforce any convention, so you can still "misplace" the quote (as in OP's example). – Jarod42 Nov 17 '20 at 11:23
  • [Delimiter tens and units in c++ literals](https://stackoverflow.com/q/46254084/995714), [Is there a way to write a large number in C++ source code with spaces to make it more readable?](https://stackoverflow.com/q/50559636/995714), [Making large constants in C source more readable?](https://stackoverflow.com/q/10977260/995714) – phuclv Nov 17 '20 at 11:28
  • Does this answer your question? [Representing big numbers in source code for readability?](https://stackoverflow.com/questions/14220217/representing-big-numbers-in-source-code-for-readability) – phuclv Nov 17 '20 at 11:29

2 Answers2

6

Since C++14,

from https://en.cppreference.com/w/cpp/language/integer_literal

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

Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

C++ allows single quotes to separate the digits for better reading:

https://en.cppreference.com/w/cpp/language/integer_literal

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

Raildex
  • 3,406
  • 1
  • 18
  • 42