377 views

1 Answer

Best answer
2 votes
2 votes

Functions themselves are always external, because C does not allow functions to be defined inside other functions.

This is taken from C programming by Dennis Ritchie. 

There is no meaning of writing extern infront of a function, since functions are always external in C. It will compile.

Variables can be external as well as internal. Internal variables are those defined inside a function or a block. Automatic variables and register variables are always internal. Variables defined outside of any function are global variables, or external variables. They can be accessed by any function defined after the variable definition.

So, where's the use of extern keyword??

It is used to access gloabal variables defined in some other file of the same program.

FILE 1:

   extern int a;

   void func(int k);           // a function which uses the global variable a

FILE 2:

   int a = 10;                   //variable 'a' defined in FILE 2

In this case, the variable a is defined in FILE2, and used in FILE1 by func(), and before using 'a' it is declared in FILE1 using

extern int a;

For better understanding, I recommend you to go through The C Programming Language by Dennis Ritchie, page 73, section 4.3 External Variables.

selected by

Related questions

2 votes
2 votes
3 answers
1
Laahithyaa VS asked Sep 9, 2023
895 views
. What will be the value returned by the following function, when it is called with 11?recur (int num){if ((num / 2)! = 0 ) return (recur (num/2) *10+num%2);else return 1...
1 votes
1 votes
3 answers
3
jverma asked May 23, 2022
1,030 views
#include <stdio.h>int f(int n){ static int r = 0; if (n <= 0) return 1; r=n; return f(n-1) + r;}int main() { printf("output is %d", f(5)); return 0;}Ou...