I was reading The C programming language by D.R. & B.K. I am not getting the meaning of the following line.Kindly help me with the example.
This means that if your function refers to "global" variables (as opposed to "local" ones that are only known to that function) you don't have to declare them, provided they were defined earlier in the file. The compiler would realise that max and longest in K&R's example refer to the max and longest declared at the top of the file without being told specifically by the external declaration. So the external declaration is redundant.
You would need it if the definition occurred *after* the function in the source file.
However, all C I've seen has the "global" variables declared before the functions I've never seen any external variables re-declared inside a function.
I've put "global" in quotes here as I'm using the term in the sense of global within that file.
While the statement is correct, I think they are confusing a simple issue.
I would say simply extern is required only to distinguish a data declaration from a data instantiation. For example:
extern is always explicit for a definition not within a function.Code:extern int foo ; // declaration int foo = 255 ; // instantiation (with initialiser) extern int bar ; // declaration int bar ; // instantiation (without initialiser)
All that said, your book should hardly be encouraging teh use of global data declarations in any case. It is just poor practice.
extern is entirely unnecessary for function declarations, because these are distinguished from definitions by having no function body. They are deed for data declarations because a data declaration need not have an explicit initialiser.
Last edited by clifford; May 18th, 2013 at 12:19 PM.