Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can header exist without being a file?

Tags:

c

header-files

In C: The Complete Reference, Herbert Schildt says that

Headers are usually files, but they are not necessarily files. It is permissible for a compiler to predefine the contents of a header internaly. However for all practical purposes, the standard c headers are contained in files that correspond to their names.

How can a header exist without being a file? What's the theme of this passage? Because the .h file extension is used with a header.

like image 522
Noman Avatar asked Mar 14 '16 14:03

Noman


People also ask

Can you have a header file without a CPP file?

You don't need a cpp file. Including the header is enough. The preprocessor expands your defines with whatever is after it.

Why do header files exist?

Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .

Is a header file needed?

Yes, because it's still based on C. You can answer your own question: Don't use them and try to compile without them. If you can't, then the compilers still require them.

Are header files automatically included?

Rationale. In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor by the use of a preprocessor directive in the source file.


2 Answers

The C Standard does make a difference between header and source files referred to by #include preprocessing directives:

6.10.2 Source file inclusion

Constraints

1 A #include directive shall identify a header or source file that can be processed by the implementation.

Semantics

2 A preprocessing directive of the form

# include <h-char-sequence> new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

3 A preprocessing directive of the form

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

# include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

The compiler may implement a scheme where standard headers are not actually stored as files in the file system. But the directive #include "filename.h" is specified as searching first for a file in a system specific way, and then searching standard headers as if the directive had been #include <filename.h>

Note that the filename extensions .c and .h are purely a convention to distinguish files containing declarations and files containing actual code and data definitions. Nothing in the Standard makes this convention a requirement, beyond the names used for the standard headers. Some people use other conventions with different extensions or no extensions at all for specific needs, but the vast majority of C programmers would regard this as bad practice.

Shafik Yaghmour provided a quote from the C99 Rationale in an answer to a similar question that nails the committees intent on this matter: https://stackoverflow.com/a/27262904/4593267

like image 142
chqrlie Avatar answered Sep 20 '22 12:09

chqrlie


The C standard explicitly says that a header is a file, so I think not.

6.10.2 Source file inclusion

A #include directive shall identify a header or source file that can be processed by the implementation.

However, what counts as a file for the given system is implementation-defined. It could in theory be a hardware port, a pipe or some some other silliness. Most of the major OS have the possibility to treat ports or pipes as files (for example in Windows, files and hardware ports are opened with the same function.)

like image 25
Lundin Avatar answered Sep 19 '22 12:09

Lundin