Write a program to read the choice and print respective min and max values of the data types that is given in the choice.
<climits>
library for predefined constantsChoice 1 (Signedness): 1 → SIGNED 2 → UNSIGNED Choice 2 (Data Type): 1 → char 2 → short int 3 → int 4 → long int 5 → long long int Example: Input "1 2" ↓ ↓ SIGNED short int Output: Minimum value of signed short int is -32768 Maximum value of signed short int is 32767
First line will be choice for selecting signed/unsigned data type (SIGNED, UNSIGNED) Followed by input for the datatypes selection (char, short int, int, long int, long long int)
Minimum value of signed short int is (Value) Maximum value of signed short int is (Value)
1 <= choice1 <= 2 1 <= choice2 <= 5
#include <iostream> #include <climits> // For INT_MAX, LONG_MAX, etc. using namespace std; int main() { int choice1, choice2; cin >> choice1 >> choice2; if (choice1 == 1) { // SIGNED switch(choice2) { case 1: cout << "Minimum value of signed char is " << SCHAR_MIN << endl; cout << "Maximum value of signed char is " << SCHAR_MAX << endl; break; case 2: cout << "Minimum value of signed short int is " << SHRT_MIN << endl; cout << "Maximum value of signed short int is " << SHRT_MAX << endl; break; case 3: cout << "Minimum value of signed int is " << INT_MIN << endl; cout << "Maximum value of signed int is " << INT_MAX << endl; break; case 4: cout << "Minimum value of signed long int is " << LONG_MIN << endl; cout << "Maximum value of signed long int is " << LONG_MAX << endl; break; case 5: cout << "Minimum value of signed long long int is " << LLONG_MIN << endl; cout << "Maximum value of signed long long int is " << LLONG_MAX << endl; break; } } else { // UNSIGNED (choice1 == 2) switch(choice2) { case 1: cout << "Minimum value of unsigned char is 0" << endl; cout << "Maximum value of unsigned char is " << UCHAR_MAX << endl; break; case 2: cout << "Minimum value of unsigned short int is 0" << endl; cout << "Maximum value of unsigned short int is " << USHRT_MAX << endl; break; case 3: cout << "Minimum value of unsigned int is 0" << endl; cout << "Maximum value of unsigned int is " << UINT_MAX << endl; break; case 4: cout << "Minimum value of unsigned long int is 0" << endl; cout << "Maximum value of unsigned long int is " << ULONG_MAX << endl; break; case 5: cout << "Minimum value of unsigned long long int is 0" << endl; cout << "Maximum value of unsigned long long int is " << ULLONG_MAX << endl; break; } } return 0; }
What is climits?
The <climits>
header provides predefined constants for the limits of integral types in C++.
Common Constants:
Constant | Data Type | Typical Value |
---|---|---|
SCHAR_MIN |
signed char | -128 |
SCHAR_MAX |
signed char | 127 |
UCHAR_MAX |
unsigned char | 255 |
SHRT_MIN |
short | -32,768 |
SHRT_MAX |
short | 32,767 |
INT_MIN |
int | -2,147,483,648 |
INT_MAX |
int | 2,147,483,647 |
LLONG_MAX |
long long | 9,223,372,036,854,775,807 |
ULLONG_MAX |
unsigned long long | 18,446,744,073,709,551,615 |
What's the Difference?
Aspect | Signed | Unsigned |
---|---|---|
Can hold negatives? | ✅ Yes | ❌ No (only 0 and positive) |
Minimum value | Negative (e.g., -32768) | Always 0 |
Maximum value | Positive (e.g., 32767) | Larger positive (e.g., 65535) |
Number of bits | Same as unsigned | Same as signed |
Range symmetry | Asymmetric (|min| > |max|) | Symmetric from 0 |
Use case | General purpose, when negatives possible | Counts, sizes, never negative |
// Example: 8-bit (1 byte) types // Signed char: 8 bits, 1 bit for sign signed char sc; Range: -128 to 127 // 256 total values (-128 + 128 = 256) // Unsigned char: 8 bits, all for magnitude unsigned char uc; Range: 0 to 255 // 256 total values (0 to 255 = 256) // Same number of bits = same number of values! // But distributed differently (with/without negatives)
Type | Bytes | Bits | Signed Range | Unsigned Range |
---|---|---|---|---|
char | 1 | 8 | -128 to 127 | 0 to 255 |
short | 2 | 16 | -32,768 to 32,767 | 0 to 65,535 |
int | 4 | 32 | -2.1B to 2.1B | 0 to 4.2B |
long | 4-8 | 32-64 | Platform dependent | Platform dependent |
long long | 8 | 64 | -9.2 quintillion | 0 to 18.4 quintillion |
// Formula for ranges: // Signed type with n bits: Minimum: -2^(n-1) Maximum: 2^(n-1) - 1 // Unsigned type with n bits: Minimum: 0 Maximum: 2^n - 1 // Example: 16-bit short Signed: -2^15 to 2^15-1 = -32768 to 32767 Unsigned: 0 to 2^16-1 = 0 to 65535
Why Use Switch Instead of If-Else?
// Switch statement structure: switch(variable) { case value1: // Code for value1 break; // Important! Prevents fall-through case value2: // Code for value2 break; default: // Code if no case matches (optional) break; } // Why 'break' is crucial: switch(x) { case 1: cout << "One"; // NO BREAK! Falls through to case 2! case 2: cout << "Two"; break; } // If x=1, prints "OneTwo" (fall-through behavior)
sizeof()
<limits>
instead of <climits>