Maximum value of unsigned short int in C++ - GeeksforGeeks

28 Dec, 2020
In this article, we will discuss the unsigned short int data type in C++. It is the smallest (16 bit) integer data type in C++. 
Some properties of the unsigned short int data type are:
Being an unsigned data type, it can store only positive values.Takes a size of 16 bits.A maximum integer value that can be stored in an unsigned short int data type is typically 65535, around 216 – 1(but is compiler dependent).The maximum value that can be stored in unsigned short int is stored as a constant in <climits> header file whose value can be used as USHRT_MAX.The minimum value that can be stored in unsigned short int is zero.In case of overflow or underflow of data type, the value is wrapped around. For example, if 0 is stored in an unsigned short int data type and 1 is subtracted from it, the value in that variable will become equal to 65535. Similarly, in the case of overflow, the value will round back to zero.
Being an unsigned data type, it can store only positive values.
Takes a size of 16 bits.
A maximum integer value that can be stored in an unsigned short int data type is typically 65535, around 216 – 1(but is compiler dependent).
The maximum value that can be stored in unsigned short int is stored as a constant in <climits> header file whose value can be used as USHRT_MAX.
The minimum value that can be stored in unsigned short int is zero.
In case of overflow or underflow of data type, the value is wrapped around. For example, if 0 is stored in an unsigned short int data type and 1 is subtracted from it, the value in that variable will become equal to 65535. Similarly, in the case of overflow, the value will round back to zero.
Below is the program to get the highest value that can be stored in unsigned short int in C++:
C++
// C++ program to obtain the maximum// value that we can store in an// unsigned short int#include <climits>#include <iostream>using namespace std;  int main(){    // From the constant of climits    // header file    unsigned short int valueFromLimits = USHRT_MAX;    cout << "Value from climits "         << "constant: "         << valueFromLimits << "\n";      // using the wrap around property    // of data types      // Initialize variable with value 0    unsigned short int value = 0;      // subtract 1 from the value since    // unsigned data type cannot store    // negative number, the value will    // wrap around and store the maximum    // value that can store in it    value = value - 1;      cout << "Value using the wrap "         << "around property: "         << value << "\n";      return 0;}
Value from climits constant: 65535
Value using the wrap around property: 65535

Data Type
Data Types
C++
C++ Programs
Data Type
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Iterators in C++ STL
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Header files in C/C++ and its uses
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
CSV file management using C++
Program to print ASCII Value of a character