7

can anyone explain these lines to me:

int xyz( void )  
{ 
extern void abc( void );
}

a function declaration within a function definition? or am I missunderstanding something?

5 Answers 5

12

Yes, your guess is correct. It's declaring the existence of the function abc(), so it may be referenced within xyz(). Note that the extern is unnecessary, as functions are extern by default.

Sign up to request clarification or add additional context in comments.

5 Comments

I wouldn't call extern unnecessary. One could say that writing extern before a prototype is more correct grammer-wise, instead of having it there implicitly. Personally I always write extern before prototypes, because that gives me the habit of always writing something before them. That way I won't forget to write static before functions that should be privately encapsulated.
@Lundin: Do you write auto before all local variables?
but why declaring the existence within a function and not in the file directly (e.g. before the definition of each function). Or is it to trigger the reference to a defined moment and not during compilation?
@wander: I think the only real reason is to limit the scope of the declaration to a single function. IMHO, this isn't particularly good practice; functions should normally be declared in a header file!
That would have been rather pointless, because variables don't have prototypes. You can tell by looking at the '{' just before the variable declartion which scope it belongs in (or the lack of '{' if it is declared at file scope). When declaring a function prototype, it isn't obvious what scope the function will be in. If you forget to add static, then the code will obviously work fine still, but you have broken private encapsulation. To save myself from that, I find it good practice to always write extern/static before prototypes.
3

This way of declaration has one big advantage:

If only one or less functions are calling an external function, this declaration makes sense especially within a big source file. If a later code restructuring (function move in another file) has to be done, it is much easier to see the dependencies than to add externals on global (file) scope. In the latter case the probability of "forgetting" such externals in a file is higher. In contrast by declaring it in function scope, the declaration will move together with the function...

I also tend to do so for external global variables - the benefit comes later when maintaining and eventually restructuring / minimizing dependencies.

One last note to the topic "writing external / not external": If its just a forward declaration (-> the function is defined at end of the the same file), I would not recommend using external - because it simply isn't the case. Otherwise external makes absolute sense to indicate that definition has to be found somewhere else (or for libaries: might need to be implemented by users of that libary).

Hope this helps (as a step to a more objective oriented programming style.. :) )

Comments

2

The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function.

An extern is something that is defined externally to the current module.

It is also not uncommon to find function prototypes declared as extern.

You need it only where it's not the default, and/or where you want to specify "C" linkage.

1 Comment

Isn't extern always the default for functions in C?
1

I would just add that this construct, in my experience, is uncommon in modern code, but often seen in older code, especially "K&R" C code.

More modern code would usually get the function prototype from a header file.

Comments

0

Yes your statement is correct.....when we use extern func_name w are are declaring the func_name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.