Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to print non-printing characters with a %C specifier?

Tags:

c

Is it possible to use a function to detect non-printing characters with isctrl() and use printf with %C specifier to print them as '\n' for instance?

Or I should write an if for every control caracter and printf("\\n") for instance..?

OK, thanks to All of the kind people below - it is not posible, you HAVE to specify each situation. example:

if (isctrl(char))// WRONG
 printf("%c", char);

if (char == '\n')//RIGHT, or using switch. 
 printf("\\n");
like image 853
MNY Avatar asked Feb 01 '13 14:02

MNY


People also ask

How can we print non-printing characters?

To show non-printing characters in Word, click the “Home” tab in the Ribbon. Then click the “Show/Hide Non-Printing Characters” button in the “Paragraph” button group.

What are non-printing characters in C?

Non-printable characters are parts of a character set that do not represent a written symbol or part of the text within a document or code, but rather are there in the context of signal and control in character encoding.

Which keys are used to display non-printing characters?

Answer: This is Ctrl+* (Ctrl+Shift+8 on U.S. keyboards). If you've ever turned on display of nonprinting characters unintentionally, it may have been by accidentally pressing this key combination when you were trying to type an asterisk.


Video Answer


3 Answers

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 the non-printable characters.

like image 167
Aniket Inge Avatar answered Sep 19 '22 20:09

Aniket Inge


To expand on the answer by Aniket, you could use a combination of isprint and the 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's hexadecimal value */
        printf("\\x%02x", ch);
        break;
    }
}
like image 22
Some programmer dude Avatar answered Sep 18 '22 20:09

Some programmer dude


You can determine the non-printing character, but i dont think so, you can write those characters. You can detect specific non printing characters by observing their ASCII value.

like image 30
Rakesh Burbure Avatar answered Sep 17 '22 20:09

Rakesh Burbure