Can I print non-printable characters using the% C specifier?

Is it possible to use a function to detect non-printable characters using isctrl() and use printf with the% C specifier to print them as "\ n", for example?

Or should I write if for each control and printf("\\n") for example ..?

Well, thanks to all the people who see below - this is not possible, you need to indicate each situation. Example:

 if (isctrl(char))// WRONG printf("%c", char); if (char == '\n')//RIGHT, or using switch. printf("\\n"); 
+6
source share
3 answers

To extend the answer from Aniket, you can use a combination of isprint and a switch-statement solution:

 char ch = ...; if (isprint(ch)) fputc(ch, stdout); /* Printable character, print it directly */ else { switch (ch) { case '\n': printf("\\n"); break; ... default: /* A character we don't know, print it hexadecimal value */ printf("\\x%02x", ch); break; } } 
+7
source
 const char *pstr = "this \t has \v control \n characters"; char *str = pstr; while(*str){ switch(*str){ case '\v': printf("\\v");break; case '\n': printf("\\n"); break; case '\t': printf("\\t"); break; ... default: putchar(*str);break; } str++; } 

this will print non-printable characters.

+12
source

You can define a non-printable character, but I don’t think so, you can write these characters. You can detect certain non-printable characters by observing their ASCII value.

+1
source

All Articles