Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate digits in numeric constants (i.e. 10,000) in C or C++ code [duplicate]

Tags:

c++

c

literals

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?

like image 738
Igorzovisk Avatar asked Sep 09 '16 13:09

Igorzovisk


People also ask

What is numeric constants in C?

A numeric constant consists of numerals, an optional leading sign, and an optional decimal point. Examples of valid numeric constants are: 5.0, 6, -5. Examples of invalid numeric constants are: 1-, 1A, 3..

How do you write large numbers in CPP?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections.

How do you separate the digits in a numeric constant in Java?

Java allows the digits in a numeric constant to be separated as follows: Does C or C++ have a similar construct? Eugene Sh. The only way to do this is in C++14, is with single quotes, like this.

How to split each digit from a number?

Now, we have to split the digit 1 from number 12. This can be achieved by dividing the number by 10 and take the modulo 10. Using above method, we can split each digit from a number.

What is an integer constant?

An integer constant refers to a sequence of digits without a decimal point. An integer preceded by a unary minus may be considered to represent a negative constant It consists of any combinations of digits taken from the set 0 through 9, preceded by an optional – or + sign. The first digit must be other than 0.

What are the different types of numeric constants?

Numeric constants: There are two types of numeric constants, Integer constants Real or floating-point constants Integer constants Any whole number value is an integer. An integer constant refers to a sequence of digits


2 Answers

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)

like image 90
Arnav Borborah Avatar answered Oct 13 '22 03:10

Arnav Borborah


You may write in C++ 14

int a = 1'000'000;

In C such a feature is absent.

like image 18
Vlad from Moscow Avatar answered Oct 13 '22 03:10

Vlad from Moscow