The maximum and minimum size of integral values are quite useful or in simple terms, limits of any integral type plays quite a role in programming. Instead of remembering these values, different macros can be used.
<climits>(limits.h) defines sizes of integral types.
- This header defines constants with the limits of fundamental integral types for the specific system and compiler implementation used.
- The limits for fundamental floating-point types are defined in <cfloat> (<float.h>).
- The limits for width-specific integral types and other typedef types are defined in <cstdint> (<stdint.h>).
Different macro constants are :
1. CHAR_MIN :
Minimum value for an object of type char
Value of CHAR_MIN is either -127 (-27+1) or less* or 0
2. CHAR_MAX :
Maximum value for an object of type char
Value of CHAR_MAX is either 127 (27-1) or 255 (28-1) or greater*
3. SHRT_MIN :
Minimum value for an object of type short int
Value of SHRT_MIN is -32767 (-215+1) or less*
4. SHRT_MAX :
Maximum value for an object of type short int
Value of SHRT_MAX is 32767 (215-1) or greater*
5. USHRT_MAX :
Maximum value for an object of type unsigned short int
Value of USHRT_MAX is 65535 (216-1) or greater*
6. INT_MIN :
Minimum value for an object of type int
Value of INT_MIN is -2147483648 (-231) or less*
7. INT_MAX :
Maximum value for an object of type int
Value of INT_MAX is 2147483647 ( 231-1)
8. UINT_MAX :
Maximum value for an object of type unsigned int
Value of UINT_MAX is 4294967295 (232-1) or greater*
9. LONG_MIN :
Minimum value for an object of type long int
Value of LONG_MIN is -2147483647 (-231+1) or less*
10. LONG_MAX :
Maximum value for an object of type long int
Value of LONG_MAX is 2147483647 (231-1) or greater*
11. ULONG_MAX :
Maximum value for an object of type unsigned long int
Value of ULONG_MAX is 4294967295 (232-1) or greater*
12. LLONG_MIN :
Minimum value for an object of type long long int
Value of LLONG_MIN is -9223372036854775807 (-263+1) or less*
13. LLONG_MAX :
Maximum value for an object of type long long int
Value of LLONG_MAX is 9223372036854775807 (263-1) or greater*
14. ULLONG_MAX :
Maximum value for an object of type unsigned long long int
Value of ULLONG_MAX is 18446744073709551615 (264-1) or greater*
NOTE: The actual value depends on the particular system and library implementation but shall reflect the limits of these types in the target platform. Your Machine's values might depend upon whether it is a 32-bit machine or 64-bit machine.
Compatibility :
LLONG_MIN, LLONG_MAX, and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).
Two Applications of these MACROS are Checking for integer overflow and Computing minimum or maximum in an array of very large or very small elements.
Below Program will display the respective values for your machine:
C++
// C++ program to demonstrate working of
// constants in climits.
#include <climits>
#include <iostream>
using namespace std;
int main()
{
cout << "CHAR_MIN : " << CHAR_MIN << endl;
cout << "CHAR_MAX : " << CHAR_MAX << endl;
cout << "SHRT_MIN : " << SHRT_MIN << endl;
cout << "SHRT_MAX : " << SHRT_MAX << endl;
cout << "USHRT_MAX : " << USHRT_MAX << endl;
cout << "INT_MIN : " << INT_MIN << endl;
cout << "INT_MAX : " << INT_MAX << endl;
cout << "UINT_MAX : " << UINT_MAX << endl;
cout << "LONG_MIN : " << LONG_MIN << endl;
cout << "LONG_MAX : " << LONG_MAX << endl;
cout << "ULONG_MAX : " << ULONG_MAX << endl;
cout << "LLONG_MIN : " << LLONG_MIN << endl;
cout << "LLONG_MAX : " << LLONG_MAX << endl;
cout << "ULLONG_MAX : " << ULLONG_MAX << endl;
return 0;
}
Output (Machine dependent)
CHAR_MIN : -128
CHAR_MAX : 127
SHRT_MIN : -32768
SHRT_MAX : 32767
USHRT_MAX : 65535
INT_MIN : -2147483648
INT_MAX : 2147483647
UINT_MAX : 4294967295
LONG_MIN : -9223372036854775808
LONG_MAX : 9223372036854775807
ULONG_MAX : 18446744073709551615
LLONG_MIN : -9223372036854775808
LLONG_MAX : 9223372036854775807
ULLONG_MAX : 18446744073709551615
Similar Reads
Literals in C++
In C++ programming language, literals are fundamental elements used to represent fixed values. These values can include numbers, characters, strings, and more. They are generally present as the right operand in the assignment operation.Let's take a look at an example:C++#include <iostream> usi
6 min read
<utility> in C++
It is a header file that contains utilities in unrelated domains. Pairs: These are the objects which can hold two different types of values. Generic Relational Approach: It is used for the relational operators !=, >, = under a specific namespace: rel_ops. Generic swap function: This a standard de
5 min read
<bits/stdc++.h> in C++
It is basically a header file that includes every standard library. In programming contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time sensitive. In programming contests, people do focus more on finding the algorithm to
2 min read
system() in C/C++
The system() function is used to invoke an operating system command from a C/C++ program. For example, we can call system("dir") on Windows and system("ls") in a Unix-like environment to list the contents of a directory.It is a standard library function defined in <stdlib.h> header in C and
6 min read
main Function in C
The main function is the entry point of a C program. It is a user-defined function where the execution of a program starts. Every C program must contain, and its return value typically indicates the success or failure of the program. In this article, we will learn more about the main function in C.E
5 min read
Tokens in C
In C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r
4 min read
Macros In C++
C++ is a powerful and versatile programming language that offers many features to enhance code flexibility and maintainability. One such feature is macros, which are preprocessor directives used for code generation and substitution. Macros are an essential part of C++ programming and play a crucial
8 min read
Chrono in C++
<chrono> is a C++ header that provides a collection of types and functions to work with time. It is a part of the C++ Standard Template Library (STL) and it's included in C++11 and later versions. <chrono> provides three main types of clocks: system_clock, steady_clock, and high_resoluti
4 min read
Modules in C++ 20
In C++20, the concept of modules was introduced to improve the efficiency of the compilation process and provide better organization and encapsulation of code. Modules allow developers to divide their code into separate components, each with its own interface and implementation, and import them as n
6 min read
__has_include in C++17
The __has_include is a special operator to work with preprocessor directives that allow you to check if a particular header file is available for inclusion in your program. It is a conditional compilation feature introduced in C++17 that helps you write portable code that can handle different enviro
2 min read