Reputation: 21364
I've a doubt about a sentence in this paragraph of the standard C11:
5.1.1.1 Program structure
A C program need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing directive #include is known as a preprocessing translation unit.
At first it says that the text of a program is kept in units called source files. After it says that a source file together with headers and source files are said to be preprocessing translation unit.
So is a source file a different entity respect to a header file or a source file include both?
Upvotes: 3
Views: 414
Reputation: 1
Pedantically, a source file (in the sense of C99 or C11 standard) is not even necessarily a file (in the operating system sense). A conforming implementation could read the "source file" from a database or some constant string inside some program.
A proprietary IBM compiler in the 1990s (VisualAge???) is rumored to have obtained the source to compile from some database. Today, tinycc also provides a libtcc
library with a function tcc_compile_string
to compile a string. TinyCC is compiling very quickly into slow machine code.
Practically, there are often several source files (like .h
& .c
files) which are processed within the same compilation or translation unit.
IIUC, the funny thing about the C standards is that they don't suppose a filesystem for the implementation to have. I believe the standard don't even require a computer; you might implement the standard unethically using a bunch of human slaves, but that is immoral and inefficient.
Upvotes: 2
Reputation: 67733
A source file is any text file (or other implementation-specific entity, per Basile's answer) containing source code. This specifically includes both .c
and .h
files. It might also includes files with other suffixes, like preprocessor or generated-code files.
When you take some source file actually passed to the compiler (so usually only .c
files), that file together with (the transitive closure of) everything it #include
s forms a translation unit.
So: every translation unit is built of source files.
But: not every source file is the starting point of a translation unit.
Upvotes: 4
Reputation: 37924
The term source file (or preprocessing file) reflects to both .c
source file and .h
header. The C Standard does not differentiate them as such.
It is explicitely mentioned in K. N. King's "C Programming: A Modern Approach" book (section 15.2).
Upvotes: 1