While I was studying the C language, I was printing ASCII codes corresponding to 0 to 255.
However, while printing, an error occurred that the output stopped at 27, and after many attempts, I was able to solve it by putting a space after the character output format specifier.
I became curious and ran printf("%c]",27); the part where the problem occurred.
As a result of executing the code separately, it was found that the result of deleting a certain amount of the output strings came out. (When a character other than ‘]’ was inserted, it worked normally!)
I’m not sure why you get these results. Are there any resources I can refer to on this?
#include <stdio.h>
int main()
{
int i = 0;
printf("%c]",27);
printf("\n");
for (; i < 256; i++)
{
printf("Teeest [ %d ] = [ %c ]",i,i);
printf("\n");
}
}
The above code is the code I wrote. The output of this is:
]
Teeest [ 8 ] = [ ]
Teeest [ 9 ] = [ ]
Teeest [ 10 ] = [
]
Teeest [ 11 ] = [ ]
Teeest [ 12 ] = [ ]
]eest [ 13 ] = [
Teeest [ 14 ] = [ ]
...
Teeest [ 253 ] = [ ?]
Teeest [ 254 ] = [ ?]
Teeest [ 255 ] = [ ]
>Solution :
Your terminal gives that sequence a special meaning. Search for ANSI escape sequences or visit the Wikipedia page.
Characters 0x00..0x1F and 0x7F are control characters in the ASCII character set, and aren’t meant to be displayed. This also applies to UTF-8, which is based on ASCII.
You’ll also have problems printing bytes 0x80..0xFF to a UTF-8 terminal as these are only found in multi-byte sequences in UTF-8.