Skip to content

@technobuddha > library > Programming > Escaping

Function: escapeC()

ts
function escapeC(input: string): string;

Defined in: escape-c.ts:46

Escape a string for use in C/C++

CharacterHexEscape Sequence
NUL0x00\0 or \000[^1]
Bell0x07\a
Backspace0x08\b
Tab0x09\t
Newline0x0a\n
Vertical Tab0x0b\v
Form Feed0x0c\f
Carriage Return0x0d\r
Escape0x1b\x1b[^2] or \u001b[^3]
Double Quote0x22\"
Single Quote0x27\'
Question Mark0x3f\?
Backslash0x5c\\
Control Characters0x00-0x1f, 0x7f-0x9f\xnn or \unnnn[^3]
BMP0x0100-0xffff\unnnn
Astral0x10000-0x10ffff\Unnnnnnnn

[^1]: The sequence \0 must not be followed by a octal digit (0-7) to avoid being interpreted as a different character, \000 will be used to avoid ambiguity. [^2]: The non-standard sequence \e represents the escape character in GCC, clang and tcc. It was not added to the C standard because it has no meaningful equivalent in some character sets (such as EBCDIC). [^3]: The sequence \xnn must not be followed by a hexadecimal digit (0-9, a-f, A-F) to avoid being interpreted as a different character, \unnnn will be used to avoid ambiguity.

Parameters

ParameterTypeDescription
inputstringThe string to escape

Returns

string

the escaped string

Example

typescript
escapeC('Hello\nWorld'); // "Hello\\nWorld"
escapeC('"\\');          // "\\\"\\\\"
escapeC('\x07');         // "\\a"
escapeC('\u20ac');       // "\\u20ac"