Skip to content

Is it possible to return "weird" characters in a char?

An answer to this question on Stack Overflow.

Question

I would like to know is it possbile to return "weird" characters, or rather ones that are important to the language
For example: \ ; '
I would like to know that because I need to return them by one function that's checking the unicode value of the text key, and is returning the character by it's number, I need these too.
I get a 356|error: missing terminating ' character
Line 356 looks as following

return '\';

Ideas?

Answer

To get a plain backslash use '\\'.

In C the following characters are represented using a backslash:

  • \a or \A : A bell
  • \b or \B : A backspace
  • \f or \F : A formfeed
  • \n or \N : A new line
  • \r or \R : A carriage return
  • \t or \T : A horizontal tab
  • \v or \V : A vertical tab
  • \xhh or \Xhh : A hexadecimal bit pattern
  • \ooo : An octal bit pattern
  • \0 : A null character
  • " : The " character
  • ' : The ' character
  • \\ : A backslash (\)

A plain backslash confuses the system because it expects a character to follow it. Thus, you need to "escape" it. The octal/hexadecimal bit patterns may not seem too useful at first, but they let you use ANSI escape codes.

If the character following the backslash does not specify a legal escape sequence, as shown above, the result is implementation defined, but often the character following the backslash is taken literally, as though the escape were not present.