0
#include <stdio.h>
int main() {
    unsigned char var = -100;
    for(var = 0; var <= 255; var++){
       printf("%d ", var);
    }
}

the output is attached below (run on codeblocks IDE version 16.01)

enter image description here

why is the output an infinite loop?

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
BATspock
  • 142
  • 1
  • 10
  • 7
    When you count on your fingers, can `n <= 10` be false? – M Oehm Sep 22 '17 at 09:28
  • 1
    same bug as the "pacman killscreen" :) – Jean-François Fabre Sep 22 '17 at 09:29
  • The range of unsigned char is 0 to 255 here it is assigned to -100 . So it should be an error why an infinite loop? – BATspock Sep 22 '17 at 09:30
  • I think you mean `for(var=0;var<255;var++)` – Jean-François Fabre Sep 22 '17 at 09:30
  • Also, the `"%d"` format specifier is for signed integers so `printf("%d ",var);` with an `unsigned char` is formally undefined behavior. That could theoretically *also* cause an infinite loop, or something else. – Bo Persson Sep 22 '17 at 10:26
  • @BoPersson sure of that? what about integer promotion? (honest question) – Jean-François Fabre Sep 22 '17 at 11:38
  • When `var == 255` and then executes `var++`, what value do you think `var` now has: 256 or 0, like it prints outs? – chux - Reinstate Monica Sep 22 '17 at 12:27
  • @BoPersson "printf("%d ",var); with an unsigned char is formally undefined behavior." is true only when `var > INT_MAX`, perhaps with a 64-bit `char` of a graphics processor. See C11 §6.5.2.2 6 "except for the following cases: — one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types" – chux - Reinstate Monica Sep 22 '17 at 12:31
  • @Jean-FrançoisFabre - It depends on the relative sizes of the built-in types, but `"%d"` is for signed values and `unsigned char` is not *guaranteed* to be promoted to a signed integer type. – Bo Persson Sep 22 '17 at 12:37

4 Answers4

4

This condition var <= 255 is always true for an unsigned char, assuming CHAR_BIT is 8 on your platform. So the loop is infinite since the increment will cause the variable to wrap (that's what unsigned arithmetic does in C).

This initialization:

unsigned char var = -100;

is not an error, it's simply annoying code. The compiler will convert the int -100 to unsigned char, following the rules in the language specification.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
unwind
  • 391,730
  • 64
  • 469
  • 606
1

Because unsigned char overflow problem. So, remove = in for loop condition.

for(var=0;var<255;var++){

}

For more information, See this stack overflow question.

Jayesh
  • 4,755
  • 9
  • 32
  • 62
1

You are using an unsigned char and its possible range is 0-255.

You are running your loop from 0-255 (inclusive). The moment your variable goes to 256, it will be converted back to 0. Also, initial value -100 will be treated as +156, due to this possible range.

So, this leads to an infinite loop.

Amber Beriwal
  • 1,568
  • 16
  • 30
  • I was wrong: "A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type." from the standard: https://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is. It's implementation defined for _signed_ types. – Jean-François Fabre Sep 25 '17 at 11:40
1
    unsigned char

Range is 0 to 255.When var =255.When it is incremented we get value as 256 which cannot be stored in unsigned char.That is the reason why it is ending in infinite loop.And when you initialize var as -100.It will not show any error because it converts -100 to binary and takes the 1st eight bits.And the corresponding value will be the value of var