user8329645
user8329645

Reputation:

while loop in c does not end

When I run this code, it enters an indefinite loop. Where did I go wrong?

#include <stdio.h>
int main()
{
    char x;                
    x=0;
    while(x<=225)
    {
        printf("%c=%d\n",x,x);
        x=x+1;
    }
    return 0;
}

Upvotes: 1

Views: 69

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

If the type char behaves as the type signed char then the valid range of values is [-128, 127]. It is undefined behavior when after increment the result value exceeds 127.

Instead of the type char you can use the type unsigned char.

Upvotes: 2

Related Questions