why #include directive doesn't have a semi-col

2019-06-18 19:51发布

In C, semicolons (;) are used to indicate the end of the statement. Why do #include lines not need a semicolon?

标签: c include
5条回答
孤傲高冷的网名
2楼-- · 2019-06-18 20:13

#include (and all other lines beginning with # like #define) is part of the preprocessor. This is actually a separate programs that runs before the main compiler and does things like include files into the source and macro expansion.

查看更多
倾城 Initia
3楼-- · 2019-06-18 20:14

The directive is processed by the preprocessor It is not a compiler, it is a simple text substitution processor. Which uses end-of-line (\n) as a significant character, unlike the C compiler which just treats it as whitespace. Also the reason that \ at the end of the line has an effect.

查看更多
狗以群分
4楼-- · 2019-06-18 20:16

#include is processed by the pre-processor and the compiler doesn't see these statements. Hence ; is not required at the end of statement.

查看更多
beautiful°
5楼-- · 2019-06-18 20:19

Because preprocessing directives are not statements.

Even not all statements are required to have a final ;. For example:

int bla = 1;

if (bla) {
}

After the declaration of bla we have two statements: one if statement and one empty compound statement. There is no ; but the program is valid.

查看更多
聊天终结者
6楼-- · 2019-06-18 20:22
#include "whatever.h"

It just replaces that line in your source file with "whatever.h". So you don't need to put ; at the end of "whatever.h". The pre-processor will give you a warning and ignore it.

查看更多
登录 后发表回答