I was trying to print the Ascii chars and I found that using char c = NUM; printf("%c", c);
would work for some numbers (tried the Ascii numbers for some of the alphabet) but some would cause a run time error, why is that?
#include <stdio.h>
int main(void) {
for(int i = 0; i < 128; i++){
printf("%c", i); // WORKS FINE
}
for(char c = '!'; c < '~'; c++){
printf("%c", c); //WORKS FINE
}
#if 0
for(char d = 0; d < 128; d++){
printf("%c", d); //IF REMOVED #if 0 --> RUNTIME ERROR
}
#endif
// HOWEVER
char e = 105;
printf("%c", e); //OK
return 0;
}
So, why is the last part working while the third for loop is not?
EDIT
After seeing the answers below I understand that my loop was ill conditioned and I should have stopped it at 127 and not 128. However, when I tried this change - it gave no output at all:
for(unsigned char c = 0; c < 255; c++){
printf("%c", c);
}
AND
for(char c = -128; c < 127; c++){
printf("%c", c);
}