0

Recently I have come across a couple of header files like

 #include <sys/socket.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <bits/stdc++.h>

I wanted to know the actual reason as to why '/' is used.

So far I have taken it as an operation to include the sub-header file of a bigger header file just like Java

 import java.util.Scanner;

but
If you remove the '/' and try to include the header file as whole
It shows an error that header file doesn't exists.

Kindly clear the confusion regarding this problem.

Thanks in advance.

Ryuzaki
  • 95
  • 1
  • 9

3 Answers3

4

Directory separation. For example, there is a directory sys which has a header file called socket.h.

And as paths in the filesystem, it can be multiple levels. You can have #include <some/sub/directory/with/a/file.h>.

The actually use of slash (/) versus backslash (\) (or anything else really) is not standardized, but is depending on the system. However all major platforms support slash (/) as separators in paths so it's most common.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I see , so the first word represented the directory. thank you very much. – Ryuzaki Oct 31 '16 at 13:11
  • 1
    Note that strictly speaking, the C++ standard says nothing on how these strings are handled. It just so happens that (nearly?) all compilers currently in use, interpret them as filenames including directories. – rubenvb Oct 31 '16 at 13:15
  • @Ryuzaki As rubenvb commented, it doesn't really have to be directories. It just makes sense that it is, and that the whole "path" is part of an actual path in a filesystem. – Some programmer dude Oct 31 '16 at 13:19
3

Files on your hard drive are organised into directories (or "folders").

The full name of a file is identified by its path, which may name several of these directories, separated by a slash character (/) or a backslash character (\).

Read more here:

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • The backslash character works only on MS Windows AFAIK, while slash works in whole *NIX world plus the windows support it too. So for source portability reasons I would stay with slash only. – Ped7g Oct 31 '16 at 13:49
  • @Ped7g: As would I. I am simply explaining what directories are, since the OP doesn't seem to know. – Lightness Races in Orbit Oct 31 '16 at 13:50
1

When you build a c++ file, the preprocessor looks for the files you specified to include in a set of directories (default ones + directories you pass on the command line).

For each #include <something> it tries to open BASE_DIR/something where BASE_DIR is one of the aforementioned directories.

So sys/socket.h denoted the preprocessor should try and delve into the subdirectory sys and open socket.h

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458