I'm trying to write a program that'll print out all ASCII characters using for loops, but it keeps going on infinite loop unless I take out 127, am I doing something wrong or is that how ASCII behaves?
This will crash (infinite loop):
#include <stdio.h> int main (void) { for (char i = -128; i <= 127; i++) { printf("%d = %c\n", i, i); } return 0; } But this is fine:
#include <stdio.h> int main (void) { for (char i = -128; i < 127; i++) { printf("%d = %c\n", i, i); } printf("%d = %c\n", 127, 127); return 0; }
i <= 127is always true ifiis a signed 8-bitchartype.i <= 127is always true, and you should investigate such warnings for possible bugs in your code.