为什么在#include 指令的文件名之后写任何东西不会在 C 程序中产生任何错误?
Why writing anything after the fileName of a #include directive does't give any errors in a C program?
为什么在 #include
指令的 fileName
之后不写任何东西在 C 程序中给出任何错误?
#include <fileName.h> we can write anything in here and it will not give an error after program compilation
main() {
printf("Hello World");
}
这是另一个例子:
#include "fileName.h" we can write here anything and its fine this will not give an error after compilation
main() {
printf("Hello World");
}
如果您从 C 规范 中找到有关此行为的任何信息,请阅读文档没有帮助,请让我知道这是 link C Documentation
包含文件后不应有任何文字。
关于 #include
的 C 标准第 6.10.2 节指出:
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 这些形式允许在包含的文件名之后添加文本。事实上,在这种情况下,gcc 和 MSVC 都会发出警告。
鉴于此代码:
#include <stdio.h> bogus text
int main()
{
return 0;
}
gcc 4.8.5 输出:
x1.c:1:20: warning: extra tokens at end of #include directive [enabled by default]
#include <stdio.h> bogus text
^
并且 MSVC 2015 输出:
x1.c
x1.c(1): warning C4067: unexpected tokens following preprocessor directive - expected a newline
为什么在 #include
指令的 fileName
之后不写任何东西在 C 程序中给出任何错误?
#include <fileName.h> we can write anything in here and it will not give an error after program compilation
main() {
printf("Hello World");
}
这是另一个例子:
#include "fileName.h" we can write here anything and its fine this will not give an error after compilation
main() {
printf("Hello World");
}
如果您从 C 规范 中找到有关此行为的任何信息,请阅读文档没有帮助,请让我知道这是 link C Documentation
包含文件后不应有任何文字。
关于 #include
的 C 标准第 6.10.2 节指出:
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 这些形式允许在包含的文件名之后添加文本。事实上,在这种情况下,gcc 和 MSVC 都会发出警告。
鉴于此代码:
#include <stdio.h> bogus text
int main()
{
return 0;
}
gcc 4.8.5 输出:
x1.c:1:20: warning: extra tokens at end of #include directive [enabled by default]
#include <stdio.h> bogus text
^
并且 MSVC 2015 输出:
x1.c
x1.c(1): warning C4067: unexpected tokens following preprocessor directive - expected a newline