4

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"
eyelash
  • 3,197
  • 25
  • 33
  • 3
    The general best practice is to never write paths in #includes in the first place, even when they are relative. It's a source of problem and non-portable. Solve file locations by external means - these days typically by adding all files to a project in a programming IDE. – Lundin Aug 24 '23 at 09:48
  • 1
    By the way the reason I'm asking this question is because I'm working on syntax highlighting for C code and I'd like to highlight include directives correctly. So renaming the file is not an answer that works for me. – eyelash Aug 24 '23 at 09:53
  • @eyelash Well if you are making program exclusively for Windows you do not have to bother with those characters because they are forbidden in file names on Windows. See the: [link](https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names) – Kesto2 Aug 24 '23 at 09:55
  • 2
    rename your include files to something that does not need escaping in thre first place – Pepijn Kramer Aug 24 '23 at 10:08
  • 1
    Also don't tag "C" and "C++". They are different languages (C++ only has some backward compatibilty with "C"). Use the tag that fits your compiler. – Pepijn Kramer Aug 24 '23 at 10:09
  • 1
    At least using such characters in filename is a very bad idea. If you can avoid, avoid. – Jean-Baptiste Yunès Aug 24 '23 at 11:01
  • I wouln't bother. There are probably only a few people in the world that are crazy enough to use \ and " in filenames. – Jabberwocky Aug 24 '23 at 11:52

3 Answers3

8

> 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.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
3

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".

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

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"
lematthias
  • 61
  • 3