Can preprocessor directives, such as #include, be placed only at the top of program code?

I used the internal #pragma functions inside the function without errors or warnings (especially #pragma pack() ). But the following code shows a warning incompatible implicit declaration of built-in function 'printf'| :

 int main(void) { printf("Trial"); } #include<stdio.h> 

Next, here is an excerpt from the book I have. The author has bad reviews about SO, especially for his generous use of void main() , but still I don’t feel that no author can be that bad to claim the following without a reason:

Each of these preprocessor directives begins with a C # character. Directives can be placed anywhere in the program, but are most often posted at the beginning of the program, before defining the first function.

So, can you tell me if you need to use some preprocessor directives, such as #include at the top of the program, while others, such as #pragma , can be used anywhere in the program?

Edit After OUAH comment I tried the following, but it does not give a warning, it gives a big pile of .LOL errors .

 int main(void) { #include<stdio.h> printf("Trial"); } 
+4
source share
3 answers

The #include directive can be placed anywhere in the source file, but in C the identifier usually cannot be used until it is declared. This is the reason you specify the #include directive at the beginning of the source file.

 void foo(void) { printf("Hello world\n"); } extern int printf(const char *, ...); // Error, the declaration should be put // before the actual call 
+5
source

Think of it this way. The contents of the included file are simply inserted at the point in the file where the #include directive appears. The resulting code must be syntactically correct for the language in which you are programming.

Remember the following file:

 int a; int foo(); int main() #include "myheader.h" int foo() { return 0; } 

And the myheader.h file contains:

 { return foo(); } 

The code that the compiler will see after the preprocessor has processed the #include directive:

 int a; int foo(); int main() { return foo(); } int foo() { return 0; } 

This is valid C. syntax. Using the #include directive is not recommended, but it gives you an idea of ​​what that means. If the file myheader.h had the following content:

 this is some garbage which is not proper C 

Then the resulting code will be:

 int a; int foo(); int main() this is some garbage which is not proper C int foo() { return 0; } 

You can use #include anywhere in the file. This leads to the literal inclusion of the contents of the included file at this point. The reason you get an undeclared message for printf () in your code is because C requires the function to be declared before use, and stdio.h has this declaration. That is why it should be prior to its use. And why it cannot be included in main () in the last example, because when it is turned on (expanded), this leads to syntactically incorrect C code.

+5
source
Directive

"# pragma" will be ignored by the C compiler because it treats lines with the "#" tag as comments. It looks like you are using openmp. The OpenMP compiler treats these (#pragma) as parallel directives. Hope this helps.

0
source

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


All Articles