Question

I was reading the gcc gnu-online-docs. I am confused about what it mentions regarding a \ or /in a header file name.

It says:

However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes. (Some systems interpret ‘\’ as a pathname separator. All of these also interpret ‘/’ the same way. It is most portable to use only ‘/’.)

What does it mean by "some systems" in this paragraph? Does it mean the implementation depends upon the OS - Windows/Linux? (I know in #include <linux/module.h>, / specifies a path)

Was it helpful?

Solution

On Windows, both / and \ function as pathname component separators (dividing the name of a directory from the name of something within the directory). On basically all other operating systems in common use today, only / serves this function.1

By taking \ literally in header-file names, instead of an escape character as it is in normal strings, GCC's preprocessor accommodates Windows-specific code written with backslashes, e.g.

#include <sys\types.h>

where it is exceedingly unlikely that the programmer intended to include a file whose name is 'sys ypes.h' (that blank before the 'y' is a hard tab). The text in the manual is intended to inform you that such code will not work if moved to a Unixy system, but that if you write it with a forward slash instead, it will work on Windows.

I happen to have written the paragraph you quote, but that was more than ten years ago now, and I don't remember why I didn't use the word "Windows".

1 VMS and some IBM mainframe OSes have entirely different pathname syntax, but these have never been well-supported by GCC, and it is my understanding that surviving installations tend to have a POSIX compatibility layer installed anyway.

OTHER TIPS

Remember that in ordinary strings, \n is a newline, \t is a tab, \a is an alert, etc.

The text means that if you write:

#include <sys\alert.h>

the \a sequence is treated as two characters, backslash and 'a', and not as a single character 'alert'. That is, the file is called alert.h and is found in a directory sys somewhere in one of the directories that the compiler searches for headers. Normally, inside a string, "sys\alert.h" would mean a name 's', 'y', 's', backspace, 'l', 'e', 'r', 't', '.', 'h'.

Similarly for:

#include <sys\backtrack.h>
#include <sys\file.h>
#include <sys\newton.h>
#include <sys\register.h>
#include <sys\time.h>
#include <sys\vtable.h>

(where I made up names as seemed to be convenient, and the sys can be replaced by any other directory name, and the <> by "").

#include <sys\386.h>
#include <sys\xdead.h>
#include <sys\ucafe.h>
#include <sys\U00DEFACED.h>

are also treated as regular strings rather than containing octal, hexadecimal, or Unicode escapes.

Windows is the main system where \ is used as the formal path element separator. However, even on Windows, the API treats / as if it were \.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top