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.
source share