Let's say I have a header file called a"b.h or a>b.h, how do I escape the " or > character in an include directive?
// this does not work
#include "a\"b.h"
Let's say I have a header file called a"b.h or a>b.h, how do I escape the " or > character in an include directive?
// this does not work
#include "a\"b.h"
> character is not allowed as part of filename when using #include <filename> directive (and respectively, " is not allowed as part of filename in #include "filename"). There is no standard way to escape any characters in the sequence, any such feature would be a compiler extension. cppreference link.
For the specific case of filename a>b.h, you can change it to #include "a>b.h" and it should work.
Filename a"b.h is out of luck - even in #include <a"b.h> it's implementation-defined how " character is treated (or if supported at all).
Aside from ", also using characters ', \, // or /* in filename part of #include directive is implementation-defined and may be unsupported.
Digging into the depths of the C grammar, this syntax is defined in C17 6.4.7. The two valid syntax items that may appear behind #include are:
header-name:
<h-char-sequence>
"q-char-sequence"
These consist of h-char and q-char items respectively. The standard then explicitly forbids > and " characters inside the inclusion strings:
h-char:
any member of the source character set except the new-line character and>
q-char:
any member of the source character set except the new-line character and"
This is portable behavior that any C compiler must follow.
However, you could use the > inside an #include "header.h".
Allowed characters are defined in lex.charset. You may use any of these characters as long as they do not break the parsing: lex.header.
In your case you may need to add the include path and try this:
#include "a>b.h"