Open In App

Maximum value of signed char in C++

In this article, we will discuss the signed char data type in C++.

Some properties of the signed char data type are:



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




// C++ program to obtain the maximum value
// that we can store in signed char
#include <climits>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
  
    // From the constant of climits
    // header file
    signed char valueFromLimits = SCHAR_MAX;
    cout << "Maximum value from "
         << "climits constant: "
         << (int)valueFromLimits
         << '\n';
  
    valueFromLimits = SCHAR_MIN;
    cout << "Minimum value from "
         << "climits constant: "
         << (int)valueFromLimits
         << '\n';
  
    // Using the wrap around property
    // of data types
  
    // Initialize two variables one
    // value with -1 as previous and
    // one with 0 as present
    signed char previous = -1;
    signed char present = 0;
  
    // Keep on increasing both values
    // until the present increases to
    // the max limit and wraps around to
    // the negative value i.e., present
    // becomes less the previous value
    while (present > previous) {
        previous++;
        present++;
    }
  
    cout << "Maximum value using the "
         << "wrap around property: "
         << (int)previous << '\n';
  
    cout << "Maximum value using the "
         << "wrap around property: "
         << (int)present;
  
    return 0;
}

Output:

Maximum value from climits constant: 127
Minimum value from climits constant: -128
Maximum value using the wrap around property: 127
Maximum value using the wrap around property: -128

Article Tags :