Why are there no semicolons after preprocessor directives?

If i write

#include <stdio.h>; 

no error, but warning appears during compilation

pari.c: 1:18: warning: additional tokens at the end of the #include directive

What is the reason?

+2
source share
6 answers

The reason is that preprocessor directives do not use semicolons. This is because they use line breaks to separate statements. This means that you cannot have multiple directives per line:

 #define ABC #define DEF // illegal 

But you can have one on several lines, ending each line (except the last) with a \ (or /, I forget).

+4
source

Since preprocessor directives are strings included in the code of our programs, which are not program statements, but directives for the preprocessor.

These preprocessor directives apply to only one line of code. As soon as a newline character is found, the preprocessor directive is considered to be completed. Therefore, at the end of the preprocessor directive, a semicolon (;) is not expected.

+3
source

Preprocessor directives are different languages ​​than C, and have a much simpler grammar, because they were initially “parsed”, if you can call it, by another program called cpp , before the C compiler saw the file. People can use this to pre-process even non-C files to include conditional parts of configuration files, etc.

There is a Linux program called "unifdef" that you can still use to remove some conditional parts of the program if you know that they will never be true. For example, if you have code to support standard non-ANSI compilers surrounded by #ifdef ANSI/#else/#end or just #ifndef ANSI/#end , and you know that you no longer need to support non-ANSI, you can eliminate dead code by running this through unifdef -DANSI .

+3
source

Because they are not needed. Preprocessor directives exist on only one line, unless you explicitly use a line continuation character (for example, a large macro).

+2
source

and if you use #define MACRO(para) fun(para); , It may be WRONG to put a half-column behind you.

 if (cond) MACRO (par1); else MACRO (par2); 

leads to a syntax error

+1
source

During compilation, your code is processed by two separate programs, a preprocessor and a compiler. The preprocessor works first.

Your code actually consists of two languages, one superimposed on top of each other. The preprocessor deals with one language, which is all directives starting with "#" (and the consequences of these directives). It processes "#include", "#define" and other directives and leaves the rest of the code intact (well, except as a side effect of preprocessor directives such as macro substitutions, etc.).

Then the compiler comes and processes the output generated by the preprocessor. It deals with the "C" language and largely ignores preprocessor directives.

The answer to your question is that "#include" is part of the language processed by the preprocessor, and in that language ";" are not required and, in fact, are “additional tokens”.

+1
source

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


All Articles