When there is a numerical value in an application’s development code, a professional programmer needs to understand which type of a variable should be used how big it could be in terms of capacity and memory usage. The developer must take into account what the minimum and maximum ranges could be. In most operations, the exact choice of variable might not be too important, but for larger numbers, it is often vital to understand the most appropriate choice to make to avoid overflow or errors where limits have been exceeded which can sometimes be difficult to track down. C++ is a great programming language that has many useful libraries and headers such as the <limits> header that helps developers to learn numerical limits of variables when the C++ compiler creates the program and it is run by the user.
In this post we explain how to use limits header to detect limits of some of the variables in C++.
Table of Contents
What is the Limits <limits> Header in C++?
The Standard library header <limits>
is a part of the type support library. There are 3 important declarations in the limits header, these are:
- numeric_limits is a class template that provides an interface to query properties of all fundamental numeric types.
- float_round_style is an enum that indicates floating-point rounding modes.
- float_denorm_style is an enum that indicates floating-point denormalization modes.
How to learn limits of a variable type in C++
How to learn limits of a variable type in C++
The Standard library header <limits>
has the numeric_limits template that can be used to learn limits of most of the arithmetic variable types in C++ like int, float
, double
, unsigned long int
, etc. In other words, the std::numeric_limits
class template provides a standardized way to learn various properties of arithmetic types, i.e you can learn the minimum and maximum value of a given type. This result is provided by the specializations of the std::numeric_limits
template.
If you want to learn limits of an arithmetic variable type in C++, you must use numeric_limits
template and its min()
, max()
methods to get minimum and maximum arithmetic values of this type.
Here is the syntax for the min()
and max()
methods of numeric_limits (since C++11)
1 2 |
static constexpr T min() noexcept; static constexpr T max() noexcept; |
for example you can learn limits of a unsigned int as below,
1 2 |
std::cout << std::numeric_limits<unsigned int>::min() << std::endl; std::cout << std::numeric_limits<unsigned int>::max() << std::endl; |
Is there a simple example of how to learn limits of a variable type in C++?
Here is a simple example of how to learn limits of a variable type in C++.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <limits> int main() { std::cout << "Limits of unsigned int : " << std::numeric_limits<unsigned int>::min() << " ... " << std::numeric_limits<unsigned int>::max() << std::endl; system("pause"); return 0; } |
and the output will be,
1 |
Limits of unsigned int : 0 ... 4294967295 |
while these limits are mostly in general, these may depend on your compiler, compiler version and the device type that you compile to use. Thus, these are useful methods to understand limits of your compiler in usage.
Is there a full example of how to learn limits of a variable type in C++?
Here is a full example, of how to learn limits of a variable type in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include <iostream> #include <limits> #include <cstdint> int main() { std::cout << "Limits of unsigned char : " << std::numeric_limits<unsigned char>::min() << " ... " << std::numeric_limits<unsigned char>::max() << std::endl; std::cout << "Limits of int : " << std::numeric_limits<int>::min() << " ... " << std::numeric_limits<int>::max() << std::endl; std::cout << "Limits of unsigned int : " << std::numeric_limits<unsigned int>::min() << " ... " << std::numeric_limits<unsigned int>::max() << std::endl; std::cout << "Limits of short int : " << std::numeric_limits<short int>::min() << " ... " << std::numeric_limits<short int>::max() << std::endl; std::cout << "Limits of unsigned short int : " << std::numeric_limits<unsigned short int>::min() << " ... " << std::numeric_limits<unsigned short int>::max() << std::endl; std::cout << "Limits of long int : " << std::numeric_limits<long int>::min() << " ... " << std::numeric_limits<short int>::max() << std::endl; std::cout << "Limits of unsigned long int : " << std::numeric_limits<unsigned short int>::min() << " ... " << std::numeric_limits<unsigned short int>::max() << std::endl; std::cout << "Limits of uint16_t (umsigned int 16bits) : " << std::numeric_limits<uint16_t>::min() << " ... " << std::numeric_limits<uint16_t>::max() << std::endl; std::cout << "Limits of float : " << std::numeric_limits<float>::min() << " ... " << std::numeric_limits<float>::max() << std::endl; std::cout << "Limits of double : " << std::numeric_limits<double>::min() << " ... " << std::numeric_limits<double>::max() << std::endl; std::cout << "Limits of long double : " << std::numeric_limits<long double>::min() << " ... " << std::numeric_limits<long double>::max() << std::endl; system("pause"); return 0; } |
and the output will be,
1 2 3 4 5 6 7 8 9 10 11 12 |
Limits of unsigned char : ... Limits of int : -2147483648 ... 2147483647 Limits of unsigned int : 0 ... 4294967295 Limits of short int : -32768 ... 32767 Limits of unsigned short int : 0 ... 65535 Limits of long int : -2147483648 ... 32767 Limits of unsigned long int : 0 ... 65535 Limits of uint16_t (umsigned int 16bits) : 0 ... 65535 Limits of float : 1.17549e-38 ... 3.40282e+38 Limits of double : 2.22507e-308 ... 1.79769e+308 Limits of long double : 3.3621e-4932 ... 1.18973e+4932 Press any key to continue . . . |
As we see in our runtime, this template is not compatible well in all types. Here, in the first step, we can not see numerical results of some types, i.e, char
, unsigned char
. It displays first and last chars. To avoid this, you can use this for the char types,
1 2 3 |
std::cout << "Limits of unsigned char : " << (int) std::numeric_limits<unsigned char>::min() << " ... " << (int) std::numeric_limits<unsigned char>::max() << std::endl; |
In general, it is very useful to detect limits of most of known arithmetic variable types. The std::numerical_limits
template is very useful to query properties of all fundamental numeric types.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.