External:
Those variables that are needed by many functions and files can be declared using the external variables.
The uninitialized external variables are assigned zero as their initial value.
The keyword extern is used for declaring the variables as external.
Let us understand this concept in some more detail
Suppose we are having a multi-file program which is written in three files f1.c, f2.c and f3.c as shown below:
Here in file f1.c the variable x is defined and initialized with 8. This variable can be used in main() and fun1() but it can’t be accessed by other two files f2.c and f3.c
Suppose we want the second file f2.c to access the variable x so we need to do the following declarations as shown in the figure below:
Now the variable x can be accessed and modified in both the files f1.c and f2.c and if any alterations are there then they will be reflected in both the files.
So we have seen that the declaration of any variable using extern keyword will extend its scope.
0 comments: