0

As we know that extern keyword is used for declaring variables, functions in C..
like

extern int a;

extern int temp(int,int)

but what is the meaning of using the word extern in front of function definition in C?

like

extern int temp(int a,int b)
{
   printf("Hello");
}

we also know that extern keyword is associated with the declaration of variable ,not definition. so how can extern keyword be used in front of function definition as memory for the function is allocated when we define a function... I hope , you got my question..

Thanks in advance..

  • 1
    See [this C reference](https://en.cppreference.com/w/c) and read [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Apr 21 '21 at 10:37
  • why don't you just extern the declaration and leave the definition as it is? – Shark Apr 21 '21 at 10:37
  • `As we know that extern keyword is used for declaring variables, functions in C` And for defining. Here: [even on cppreference](https://en.cppreference.com/w/c/language/extern) there's `extern int i2 = 3;`. `what is the meaning of using the word extern in front of function definition in C?` Means symbol has external linkage, the same as for declaration. `we also know that extern keyword is associated with the declaration of variable ,not definition` Well, extern is associated with _symbol_ (as in has external linkage), and definition is also a declaration. – KamilCuk Apr 21 '21 at 10:40
  • I can. But actually i want to know the actual reason behind this question. because it is mentioned i in K&R book that extern keyword can be used before function definition. – Abhishek Jaiswal Apr 21 '21 at 10:41
  • And one important fact: symbols at file scope are `extern` "by default". Writing _at file scope_ like `int temp();` or `extern int temp();` and`int a = 1;` or `extern int a = 1;` is just the same. Here examples from standard draft: [C11 6.9.1p13](https://port70.net/~nsz/c/c11/n1570.html#6.9.1p13) and [C11 6.9.2p4](https://port70.net/~nsz/c/c11/n1570.html#6.9.2p4). – KamilCuk Apr 21 '21 at 10:42
  • but when i am using "extern int a=1;" in c programme , compiler is raising an error . "'x' has both 'extern' and initializer" – Abhishek Jaiswal Apr 21 '21 at 11:08

0 Answers0