Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
}
like image 226
Rüppell's Vulture Avatar asked Dec 20 '22 06:12

Rüppell's Vulture


2 Answers

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.

like image 198
Ziffusion Avatar answered May 05 '23 12:05

Ziffusion


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
like image 23
ouah Avatar answered May 05 '23 10:05

ouah