Home > database >  What is range from -128 to 127 in character data type?
What is range from -128 to 127 in character data type?

Time:10-23

I was just reviewing data types on codecademy and saw the description about char data type. It says:

Characters typically require 1 byte of memory space and range from -128 to 127.

What is "range from -128 to 127"? For more clarification here's a screenshot:

]

CodePudding user response:

A signed 8-bit integer can represent the numbers from -128 to 127. The ASCII encoding associates those numbers with characters.

CodePudding user response:

That means that type char can store an value from -128 to 127. If you assgin it to a value bigger than 127 or lower than -128, it will result in an overflow.

For example:

#include <iostream>

int main()
{
    char a = 127; // okay
    char b = 128; // not okay, will result in a overflow
    char c = -128;// okay
    char d = -129;// not okay, will result in a overflow
    std::cout <<(int) a << ' ' << (int)b << ' ' << (int)c << ' ' << (int)d;
}
  •  Tags:  
  • c
  • Related