Can the pre-processor directives like #include be placed only at the top of the program code?

最后都变了- 提交于 2019-12-07 09:46:12

问题


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

int main(void)
{
    printf("Trial");
}

#include<stdio.h>

Further, here's is an extract from a book I have.The author has bad reviews on SO,especially for his generous use of void main(),but still I feel no author can be that bad to claim the following without reason:

Each of these preprocessor directives begin with a # symbol. The directives can be placed anywhere in a program but are most often placed at the beginning of a program, before the first function definition.

So can you tell me whether it's mandatory to use some preprocessor directives like #include at the top of the program while others like #pragma can be used anywhere in the program?

Edit After OUAH's remark I tried the following, but it doesn't give warning,it gives a big pile of errors.LOL.

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

回答1:


An #include directive can be placed anywhere in a source file, but in C an identifier can usually not be used before it has been declared. That's the reason why you put the #include directive at the begining of your source file.

void foo(void)
{
    printf("Hello world\n");
}

extern int printf(const char *, ...);  // Error, the declaration should be put 
                                       // before the actual call



回答2:


Think of it this way. The content of the included file is simply inserted at the point in the file where the #include directive appears. The resulting code needs to be syntactically correct for the language that you are programming in.

Confider the following file:

int a;

int foo();

int main()
#include "myheader.h"

int foo()
{
    return 0;
}

And the file myheader.h contains:

{
    return foo();
}

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

int a;

int foo();

int main()
{
    return foo();
}

int foo()
{
    return 0;
}

This is valid C syntax. Such use of the #include directive is not recommended, but it gives you an idea of what it means. If the myheader.h file 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 at any point in the file. It results in literal inclusion of the content of the included file at that point. The reason you get a undeclared message for printf() in your code is that C requires a function be declared before use, and stdio.h has that declaration. Which is why it needs to be before it's use. And why it cannot be included in main() in the latter example is because on inclusion (expansion), it results in syntactically incorrect C code.




回答3:


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



来源:https://stackoverflow.com/questions/16389126/can-the-pre-processor-directives-like-include-be-placed-only-at-the-top-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!