Open In App

Maximum value of unsigned char in C++

In this article, we will discuss the maximum value of unsigned char data type in C++.

Some properties of the unsigned char data type are:



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




// C++ program to obtain the maximum
// value that can be stored in an
// unsigned char
  
#include <climits>
#include <iostream>
using namespace std;
  
// Function to find the maximum value
// stored in unsigned char
void maxUnsignedChar()
{
    // From the constant of climits
    // header file
    unsigned char valueFromLimits = UCHAR_MAX;
    cout << "Value from climits "
         << "constant : "
         << (int)valueFromLimits
         << "\n";
  
    // Using the wrap around property
    // of data types
  
    // Initialize a variable with
    // value 0
    unsigned char value = 0;
  
    // Subtract 1 from value since
    // unsigned data type cannot store
    // negative number, the value will
    // wrap around and store the maximum
    // value that we can store in it
    value = value - 1;
  
    cout << "Value using the wrap "
         << "around property : "
         << (int)value << "\n";
}
  
// Driver Code
int main()
{
    // Function call
    maxUnsignedChar();
  
    return 0;
}

Output:

Value from climits constant : 255
Value using the wrap around property : 255

Article Tags :