Program
\\ Program in file externa1.c
#include <stdio.h> \\ A
#include <d:\cbook\storage\f1.cpp> \\ B
extern int i; \\ C
main()
{
i =0; \\ D
printf("value of i %d\n",i);
}
\\ Program in file f1.cpp
int i =7; \\ E
Explanation
- The file f1.cpp has the declaration of i.
- In the file extern.c there is a reference of i so the compiler should know the data type of i. This is done using the extern definition by statement C. Extern means that the variable or function is implemented elsewhere but is referred to in the current file.
- Statement D refers to i.
- The definition of i is given in the file f1.cpp, as given by statement E.
- In the absence of an include directive in statement B, you can still compile the file; it will give no errors. Such a file is called an object file. It is not fit for execution because the reference of i is not resolved.
- When you write statement B the reference of i is re-sorted and the executable file can be made
No comments:
Post a Comment