Why doesn't the #include directive have a semicolon at the end of the statement?

In C, semicolons ( ; ) are used to indicate the end of an instruction. Why don't #include lines need semicolons?

+6
source share
5 answers

#include (and all other lines starting with # as #define ) are part of the preprocessor . In fact, these are separate programs that run in front of the main compiler and do things such as including files in the source and macro extensions.

+12
source

#include handled by the pre-processor, and the compiler does not see these instructions. Therefore ; not required at the end of the instructions.

+2
source

Because preprocessing directives are not instructions.

Not even all statements must be final ; . For instance:

 int bla = 1; if (bla) { } 

After declaring bla we have two statements: one if and one empty compound statement. No ; but the program is valid.

+2
source

The directive is processed by the preprocessor. It is not a compiler, it is a simple word processor. Which uses end-of-line (\n) as a significant character, unlike the C compiler, which simply treats it as a space. The reason why \ at the end of the line also matters.

+2
source
 #include "whatever.h" 

It simply replaces this line in your source file with "whatever.h". Therefore you do not need to embed ; to the end of "whatever.h". The preprocessor will give you a warning and ignore it.

+1
source

Source: https://habr.com/ru/post/912386/


All Articles