Related Articles

# Maximum value of short int in C++

• Difficulty Level : Basic
• Last Updated : 28 Dec, 2020

In this article, we will discuss the short int data type in C++. This data type in C++ is used to store 16-bit integers.

Some properties of the short int data type are:

1. Being a signed data type, it can store positive values as well as negative values.
2. Takes a size of 16 bits where 1 bit is used to store the sign of the integer.
3. A maximum integer value that can be stored in a short int data type is typically 32767, around 215-1(but is compiler dependent).
4. The maximum value that can be stored in short int is stored as a constant in <climits> header file. Whose value can be used as SHRT_MAX.
5. The minimum value that can be stored in short int is stored as a constant in <climits> header file. Whose value can be used as SHRT_MIN.
6. A minimum integer value that can be stored in a short int data type is typically -32768 around (-215+1) (but is compiler dependent).
7. In case of overflow or underflow of data type, the value is wrapped around. For example, if -32768 is stored in a short int data type and 1 is subtracted from it, the value in that variable will become equal to 32767. Similarly, in the case of overflow, the value will round back to -32768.

Below is the program to get the highest value that can be stored in unsigned long long int in C++:

## C++

 `// C++ program to obtain themaximum``// value that can be store in short int``#include ``#include ``using` `namespace` `std;`` ` `// Driver Code``int` `main()``{``    ``// From the constant of climits``    ``// header file``    ``short` `int` `valueFromLimits = SHRT_MAX;``    ``cout << ``"Value from climits "``         ``<< ``"constant (maximum): "`` ` `         ``<< valueFromLimits << ``"\n"``;`` ` `    ``valueFromLimits = SHRT_MIN;``    ``cout << ``"Value from climits "``         ``<< ``"constant (minimum): "``         ``<< valueFromLimits << ``"\n"``;`` ` `    ``// Using the wrap around property``    ``// of data types`` ` `    ``// Initialize two variables with``    ``// -1 as previous and 0 as present``    ``short` `int` `previous = -1;``    ``short` `int` `present = 0;`` ` `    ``// Increment both values until the``    ``// present increases to the max limit``    ``// and wraps around to the negative``    ``// value i.e., present becomes less``    ``// than the previous value``    ``while` `(present > previous) {``        ``previous++;``        ``present++;``    ``}`` ` `    ``cout << ``"Value using the wrap "``         ``<< ``"around property :\n"``;``    ``cout << ``"Maximum: "` `<< previous << ``"\n"``;``    ``cout << ``"Minimum: "` `<< present << ``"\n"``;`` ` `    ``return` `0;``}`
Output:
```Value from climits constant (maximum): 32767
Value from climits constant (minimum): -32768
Value using the wrap around property :
Maximum: 32767
Minimum: -32768
```
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up