Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the backslash acceptable in C and C++ #include directives?

There are two path separators in common use: the Unix forward-slash and the DOS backslash. Rest in peace, Classic Mac colon. If used in an #include directive, are they equal under the rules of the C++11, C++03, and C99 standards?

like image 358
Potatoswatter Avatar asked Apr 26 '11 12:04

Potatoswatter


People also ask

What is backslash used for in C?

In Windows systems, for example, the backslash is used to separate elements of a file path, for example: C:\Documents\User\File. In C, Perl and Unix scripting, the backslash indicates that the following character must be treated in some special way. Within the TeX typesetting markup system, the backslash starts tags.

Does forward slash need to be escaped in C?

You don't need to escape forward slash.

What does the slash mean in C?

For example, in Windows, backslashes are commonly found as path separators. In C-based programming languages (as well as other scripting languages), they are escape characters. They indicate that the character to their right should be interpreted differently from the way it normally would.

When should a backslash be used?

The backslash is used only for computer coding. The forward slash, often simply referred to as a slash, is a punctuation mark used in English. The only time it is appropriate to use a comma after a slash is when demonstrating breaks between lines of poetry, songs, or plays.


1 Answers

C99 says (§6.4.7/3):

If the characters ', \, ", //, or /* occur in the sequence between the < and > delimiters, the behavior is undefined. Similarly, if the characters ', \, //, or /* occur in the sequence between the " delimiters, the behavior is undefined.

(footnote: Thus, sequences of characters that resemble escape sequences cause undefined behavior.)

C++03 says (§2.8/2):

If either of the characters ’ or \, or either of the character sequences /* or // appears in a q-char- sequence or a h-char-sequence, or the character " appears in a h-char-sequence, the behavior is undefined.

(footnote: Thus, sequences of characters that resemble escape sequences cause undefined behavior.)

C++11 says (§2.9/2):

The appearance of either of the characters ’ or \ or of either of the character sequences /* or // in a q-char-sequence or an h-char-sequence is conditionally supported with implementation-defined semantics, as is the appearance of the character " in an h-char-sequence.

(footnote: Thus, a sequence of characters that resembles an escape sequence might result in an error, be interpreted as the character corresponding to the escape sequence, or have a completely different meaning, depending on the implementation.)

Therefore, although any compiler might choose to support a backslash in a #include path, it is unlikely that any compiler vendor won't support forward slash, and backslashes are likely to trip some implementations up by virtue of forming escape codes. (Edit: apparently MSVC previously required backslash. Perhaps others on DOS-derived platforms were similar. Hmmm… what can I say.)

C++11 seems to loosen the rules, but "conditionally supported" is not meaningfully better than "causes undefined behavior." The change does more to reflect the existence of certain popular compilers than to describe a portable standard.

Of course, nothing in any of these standards says that there is such a thing as paths. There are filesystems out there with no paths at all! However, many libraries assume pathnames, including POSIX and Boost, so it is reasonable to want a portable way to refer to files within subdirectories.

like image 172
Potatoswatter Avatar answered Sep 20 '22 18:09

Potatoswatter