The Program in C++ to Check Whether the primitive values crossing the limits or not is given below:
#include <iostream>
#include <climits>
using namespace std;
int main() {
int a;
short b;
long c;
long long d;
cout << "Enter an integer value: ";
cin >> a;
cout << "Enter a short value: ";
cin >> b;
cout << "Enter a long value: ";
cin >> c;
cout << "Enter a long long value: ";
cin >> d;
if (a > INT_MAX) {
cout << "Integer limit exceeded!" << endl;
} else if(a < INT_MIN) {
cout << "Integer minimum limit exceeded!" << endl;
}
if (b > SHRT_MAX) {
cout << "Short limit exceeded!" << endl;
} else if(b < SHRT_MIN) {
cout << "Short minimum limit exceeded!" << endl;
}
if (c > LONG_MAX) {
cout << "Long limit exceeded!" << endl;
} else if(c < LONG_MIN) {
cout << "Long minimum limit exceeded!" << endl;
}
if (d > LLONG_MAX) {
cout << "Long long limit exceeded!" << endl;
} else if(d < LLONG_MIN) {
cout << "Long long minimum limit exceeded!" << endl;
}
return 0;
}
Output:
Enter an integer value: 200000000
Enter a short value: 30000
Enter a long value: 400000000
Enter a long long value: 50000000000
In this example, the user inputs the values 200000000 for the int, 30000 for the short, 400000000 for the long, and 50000000000 for the long long.
Since none of these values exceed the maximum limit or minimum limit for their respective data types (INT_MAX, SHRT_MAX, LONG_MAX, LLONG_MAX, INT_MIN, SHRT_MIN, LONG_MIN, LLONG_MIN), the program will not output any messages indicating that a limit has been exceeded.
However, if the user inputs a value larger than the maximum limit for a certain data type,
for example if the user inputs the value 2000000000 for the int, the program will output the message “Integer limit exceeded!” as 2000000000 > INT_MAX.
Similarly, if user inputs a value smaller than the minimum limit for a certain data type,
for example if the user inputs the value -2000000000 for the int, the program will output the message “Integer minimum limit exceeded!” as -2000000000 < INT_MIN.
Pro-Tips💡
This program prompts the user to input values for an int, a short, a long, and a long long using the cout
and cin
functions.
Then it checks if the inputted values exceed their respective maximum limits using the constants INT_MAX
, SHRT_MAX
, LONG_MAX
, and LLONG_MAX
.
If the inputted value is larger than the maximum limit, it will output a message indicating that the limit has been exceeded.
Similarly, it checks if the inputted values are less than their respective minimum limit using the constants INT_MIN
, SHRT_MIN
, LONG_MIN
, and LLONG_MIN
.
If the inputted value is smaller than the minimum limit, it will output a message indicating that the minimum limit has been exceeded.
Also, it is important to note that INT_MAX
, SHRT_MAX
, LONG_MAX
, and LLONG_MAX
constants are system dependent, so depending on the system you are using the value may vary.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.