Why doesn't writing anything after the fileName
of a #include
directive give any errors in a C program?
#include <fileName.h> we can write anything in here and it will not give an error after program compilation
main() {
printf("Hello World");
}
heres another example:
#include "fileName.h" we can write here anything and its fine this will not give an error after compilation
main() {
printf("Hello World");
}
Reading the Documentation didn't help please if you find anything about this behavior from the C specification lemme know heres the link C Documentation
There shouldn't be any text after the included file.
Section 6.10.2 of the C standard regarding #include
states:
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 searchfails, 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.4 A preprocessing directive of the form
# include pp-tokens new-line
(that does not match one of the two previous forms) is permitted. The preprocessing tokens after
include
in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a<
and a>
preprocessing token pair or a pair of"characters is combined into a single header name preprocessing token is implementation-defined.
None of these forms allows for text after the included filename. In fact, both gcc and MSVC issue warnings in this case.
Given this code:
#include <stdio.h> bogus text
int main()
{
return 0;
}
gcc 4.8.5 outputs:
x1.c:1:20: warning: extra tokens at end of #include directive [enabled by default]
#include <stdio.h> bogus text
^
And MSVC 2015 outputs:
x1.c
x1.c(1): warning C4067: unexpected tokens following preprocessor directive - expected a newline
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments