-1

Can someone explain me how is this at the end

a=?,b=?,c=-124 

and not

a=?,b=?,c=132

This is code:

#include <stdio.h>

int main() {
  char a = 'D', b='C', c='A';
  a = c + 'D';
  b = a + b + 'A';
  c = b - a;
  a = a - b + 'C';
  b = b - 68;
  printf("a=%c,b=%c,c=%d", a, b, c);
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 4
    It makes little sense to add character codes to each other. – Barmar Feb 01 '22 at 18:10
  • 2
    It would make more sense if you explained *why* you would expect `132`. – Marco Bonelli Feb 01 '22 at 18:11
  • 1
    `char` is signed on your system, so you're getting negative numbers. – Barmar Feb 01 '22 at 18:11
  • @MarcoBonelli He's expecting `132` – Barmar Feb 01 '22 at 18:11
  • 3
    [Is char signed or unsigned by default?](https://stackoverflow.com/a/2054941) – 001 Feb 01 '22 at 18:11
  • The behavior depends on your system... does it use ASCII? If so, then the char values of the ASCII values of the corresponding characters (e.g., 'A' = 65). And the results of the arithmetic depends on whether your compiler uses `signed` or `unsigned` `char`s by default. – SGeorgiades Feb 01 '22 at 18:17
  • For example a=c+'D' in compiler it show that a is -123. I just need someone to explain me in what way it happened. – mr fahrenheit Feb 01 '22 at 18:20
  • 1
    Add `printf("a=%d, b=%d, c=%d\n", a,b,c);` after each line and check the values using `signed char` and `unsigned char` and you will understand what is going on. – Laci Feb 01 '22 at 18:24
  • 1
    To see the issue, have you tried using `int` instead of `char`? In that case, you won't get rollover. With `char a = 127, b = a + 1; printf("a=%d b=%d\n",a,b);` you'll get `a=127 b=-128` but, using `int`, you'll get: `a=127, b=128` – Craig Estey Feb 01 '22 at 18:28
  • Ok. Thank you for your help. – mr fahrenheit Feb 01 '22 at 18:34

3 Answers3

2

It appears on your system that char is signed. With ASCII, 'D', 'C' and 'A' are the same as the integers 68, 67, and 65 respectively.

Add 68 to 65 and you get 133. The binary representation of 133 is 10000101. Notice that the most significant bit is 1. As you're using signed chars, twos complement comes into play, and the result is actually -123.

Remember that a signed char can hold values ranging from -128 to 127, rather than 0 to 255.

Chris
  • 26,361
  • 5
  • 21
  • 42
2

Your C implementation has a signed eight-bit char, likely uses ASCII, and, when converting an out-of-range value to a signed integer type, wraps modulo 2w, where w is the width of (number of bits in) the type. These are all implementation-defined; they may differ in other C implementations, with certain constraints.

char a = 'D', b='C', c='A'; initializes a to 68, b to 67, and c to 65.

a = c + 'D'; assigns 65 + 68 = 133 to a. Since 133 does not fit in char, it wraps to 133 − 256 = −123.

b = a + b + 'A'; assigns −123 + 67 + 65 = 9 to b.

c = b - a; assigns 9 − −123 = 132 to c. This wraps to 132 − 256 = −124.

a = a - b + 'C'; assigns −123 − 9 + 67 = −65 to a.

b = b - 68; assigns 9 − 68 = −59 to b.

printf("a=%c,b=%c,c=%d", a, b, c); prints a=?,b=?,c=-124 because a and b are codes for abnormal characters and the value of c is −124.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

It would seem your compiler treats char as signed char, so:

a = 'D', b='C', c='A'

a = c + 'D' = 'A' + 'D' = 65 + 68 = 133

but since a is a signed char, it becomes -123 (133-256)

b = a + b + 'A' = -123 + 'C' + 'A' = -123 + 67 + 65 = 9

c = b - a = 9 - -123 = 9 + 123 = 132

a = a - b + 'C' = -123 - 9 + 66 = -66

b = b - 68 = 9 - 68 = `-59``

So at the end, a = -66, b = -59, and c = 132

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11