Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I put ']' after the format specifier when printing 27th escape sequence?

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 ] = [  ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading