The Wayback Machine - https://web.archive.org/web/20240118181626/https://www.geeksforgeeks.org/maximum-value-of-signed-char-in-c/
Open In App
Related Articles

Maximum value of signed char in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

Some properties of the signed char data type are:

  • It is generally used to store 8-bit characters.
  • Being a signed data type, it can store positive values as well as negative values.
  • Size of 8 bits is occupied where 1 bit is used to store the sign of the value.
  • A maximum value that can be stored in a signed char data type is typically 127, around 27 – 1(but is compiler dependent).
  • The maximum and minimum value that can be stored in a signed char is stored as a constant in climits header file and can be named as SCHAR_MAX and SCHAR_MIN respectively.
  • A minimum value that can be stored in a signed char data type is typically -128, i.e. around –27 (but is compiler dependent).
  • In case of overflow or underflow of data type, the value is wrapped around. For example, if -128 is stored in a signed char data type and 1 is subtracted from it, the value in that variable will become equal to 127. Similarly, in the case of overflow, the value will round back to -128.

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

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 06 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads