3

Can you guy explain me how overflows and underflows works for signed char and unsigned char?

int main () {
    signed char c;

    scanf("%d",&c);
    printf("%d\n",c);
    printf("%c\n",c);

return 0;
}

In this case, if thanks to scanf, I put c=200 there is an overflow and this is showed by the first printf.

The second printf gives me the same ASCII symbol of 200...
Why?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
bigguy
  • 51
  • 2
  • 9
    `scanf("%d",&c)` is not an overflow or underflow; it is Undefined Behavior, period. – aschepler Jul 06 '16 at 19:17
  • To further complete @aschepler, the most likely outcome is clobbering 3 more bytes after the c variable, perhaps even messing with the return address itself. Assuming the compiler (optimizer) doesn't mess with stuff and it behaves similar to the manual intuitive translation. – Paul Stelian Jul 06 '16 at 19:22

3 Answers3

6

scanf's %d expects an int, so giving it anything else is undefined behavior.
You should do this:

int d;
scanf("%d", &d);
whatevertype c = (whatevertype)d;

However, signed integer overflow is undefined. But if you use unsigned types, like

unsigned char c = (unsigned char)d;

Then c is guaranteed to be d modulus 2 to the power of the number of bits in an unsigned char.

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • can you explain me what is an undefined behavior and what is the difference between overflows? – bigguy Jul 06 '16 at 20:38
  • @bigguy: You might like to have a look here: https://en.wikipedia.org/wiki/Undefined_behavior or here http://stackoverflow.com/a/18420754/694576 or here https://www.google.com/search?q=what+is+undefined+behaviour – alk Jul 07 '16 at 15:49
0

Every compiler is running on a specific machine, on a specific hardware.

Say for example that our machine/processor signed integer is in the range of 16 bits. This means that MAX_INT will be 0x7fff hex value, which is 32767 decimal value and MIN_INT will be 0x8000 hex vale, which is -32768 decimal value.

Most machines has ALU control register that defines how signed integers will behave in case of an overflow. This register generally has a saturation flag.

Overflow Example: If the saturation flag is set, than in case that the result of the last signed integer ALU operation is bigger than MAX_INT, the result will be set to MAX_INT. for example if the last operation was adding 0x7ffe to 0x2 than the result will be 0x7fff.

If the saturation flag is not set, than in case that the result of the last signed integer ALU operation is bigger than MAX_INT, the result will probably be set to the lower 16 bits of the correct result. In our case 0x7ffe+0x2=0x8000, which is the minimum integer.

In case of unsigned integers the compiler guarantees as that the result will be according to the definition of unsigned int addition in C.

Underflow example: Every machine has MIN_FLOAT definition. And again if the saturation flag is set than a result that is smaller than MIN_FLOAT will be rounded to MIN_FLOAT. other wise the result will be according to the operation of the processor. (Search in the internet to understand the terms Mantissa and exponent if you are interested to know on floating point representation and operations).

shaul boyer
  • 124
  • 3
0

The Reason you are getting a Overflow in the first printf statement is because you have defined the char as a signed char and if You print them in an %d format which is integer You'll get a range from 0-127 and -1 to -128 because as we all know char consume one byte in memory and by assigning them as signed character you'll divide them in positive and negative values in simple words a unsigned byte ranges from 0 to 255 and soo a signed character will give you both positive as well as negative value range from 0-127 and -1 to -128.

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49