简体   繁体   中英

ASCII value is greater than 127

#include <stdio.h> 
  
int main() { 
    char c = 125; 
  
    c = c + 10; 
  
    printf("%d", c); 
  
    return 0; 
}

The output of this code is -121 .

How this output is -121 ? Can you please explain?

If I add 100 instead of 10 with c then output is -31 . Why?

In C language the char type is an integer type able to represent all the characters required by the language itself. The standard does not specify whether it is a signed or unsigned type. After seeing the output, I can guess that your system uses a signed char of size 8 bits (7 value bits and one sign bit) and represent negative values in 2-complement.

So (as int) 125 + 10 = 135. 135 > 128 so the actual value is 135 - 256 = -121.

(In two complement mode, number are just wrapped over 2**SIZE_IN_BITS...)

How it works is that the type char in your compiler is a signed type which is 8 bits wide, and represents integer values using a system called two's complement.

In this system, the positive values go from 0 to 127 in binary like this:

00000000   0
00000001   1
00000010   2
00000011   3
     ...   .
01111111 - 127

Upon the next increment, the highest bit flips to 1: we get 10000000 . Then the subsequent binary values are interpreted as negative under two's complement:

10000000   128    -128
10000001   129    -127
10000010   130    -126
     ...
11111111   255      -1

The middle column, continuing to count up from 128, shows the unsigned interpretation of the 8 bit datum; it just keeps counting toward 255. That would be the behavior of the type unsigned char .

The right column, showing negative numbers, is the two's complement interpretation of exactly the same bit patterns. This instead covers a negative range of integers from -128 to -1.

Now what happens in C when we do c + 10 ?

Firstly, according to the rules of the C language, the c value of type char gets promoted to the type int , which has a much larger range. Thus the int value 125 is being added to the int value 10, resulting in the int value 135.

What happens next is that c is assigned that value, exactly as if by c = 135 . But 135 it not in range; char only goes up to 127 . Implementation-defined behavior ensues. The 135 value is somehow forced to fit, by discarding bits.

C compilers for two's complement machines (ie virtually all compilers on the planet) truncate wider integer value to narrower values simply by truncation of the bottom bits of the value to fit the smaller type.

So, the binary representation of 135 is this.

 10000111

More precisely, suppose int is 32 bits wide. Then the representation of the int value 135 is:

 00000000000000000000000010000111

This is converted to char by chopping off the top 24 bits, leaving the bottom 8:

 ------------------------10000111    

So we end up with

 10000111

in the char type. But what is that? Referring to our original table, we can add a few more rows:

10000000   128    -128
10000001   129    -127
10000010   130    -126
10000011   131    -125
10000100   132    -124
10000101   133    -123 
10000110   134    -122
10000111   135    -121 <----
     ...
11111111   255      -1

And there is the -121.

char type is represented in memory as a byte (most of the time 8 bits), so it can represent values from -128 to +127 . If you try to store a bigger value in the memory, it causes an overflow and the result is not what expected.

Here is a nice video explaning what overflow is.

The char is one byte that is 8 bit. one bit is for sign and 7 bits are all of you can use.

"char" range is: char ==> -128 ~ 127

static_cast<char>(c+1)  = 126
static_cast<char>(c+2)  = 127
static_cast<char>(c+3)  = -128

if you use from "unsigned char", positive range is increase.

unsigned char ==> 0 ~ 255

static_cast<unsigned char>(c+3) = 128

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM