Digit Separator in C++14
In this article, we will discuss the use of a digit separator in C++. Sometimes, it becomes difficult to read numbers that contain many digits. For example, 1000 is readable but what if more zeros are added to it, let’s say 1000000, now it becomes a little difficult to read, and what will happen if more zeros are added to it. In real life, commas (, ) are added to the number. For Example: 10, 00, 000. Now it is easy to read, that is ten lakhs.
Now the question arises that C++ doesn’t accept such separators (comma) so how to deal with big numbers. To deal with it, C++14 has introduced a feature and its name is Digit Separator and denoted by a simple quotation mark (‘). This can make it easier for users to read large numbers.
Program 1:
Below is the implementation to show that single quote marks are ignored when determining their value:
C++14
// C++ program to demonstrate // the above approach #include <iostream> using namespace std; // Driver code int main() { long long int a = 10 '00' 000; // Print the value cout << a; return 0; } |
1000000
Program 2:
Below is the program to show that Single quote marks are just for users. Using them in any position does not affect the compiler.
C++14
// C++ program to demonstrate // the above approach #include <iostream> using namespace std; // Driver Code int main() { long long int a = 1 '23' 456; long long int b = 12 '34' 56; long long int c = 123'456; // Print all the value cout << "a:" << a << endl; cout << "b:" << b << endl; cout << "c:" << c << endl; return 0; } |
a:123456 b:123456 c:123456
Please Login to comment...