Declaring function C as part of another function

can someone explain these lines to me:

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

function declaration in function definition? or am I not understanding something?

+6
c function declaration
source share
5 answers

Yes, your assumption is correct. It announces the existence of the abc() function, so it can be referenced in xyz() . Note that extern not required since the default function is extern .

+11
source share

Declaring "extern" in C means indicating the existence and type of a global variable or function.

External is what is defined external to the current module.

You can also often find prototypes of functions declared extern.

You only need this where it is not the default, and / or where you want to specify the "C" link.

+2
source share

This ad method has one big advantage:

If only one or more functions call an external function, this declaration makes sense, especially in a large source file. If you need to perform a later restructuring of the code (moving the function in another file), it is much easier to see the dependencies than adding external resources to the global (file) area. In the latter case, the probability of “forgetting” such external values ​​in the file is higher. On the contrary, declaring it in the function area, the declaration will move along with the function ...

I also tend to do this for external global variables - the advantage comes later when saving and, ultimately, restructuring / minimizing dependencies.

Last note on the topic “recording external / non-external”: If its just a forward declaration (-> the function is defined at the end of the same file), I would not recommend using an external one - because it is simply not so. Otherwise, it makes sense to indicate externally that the definition should be found somewhere else (or for libaries: perhaps it should be implemented by users of this library).

Hope this helps (as a step towards a more objective style of programming .. :))

+2
source share

Yes, your statement is correct ..... when we use extern func_name w, declare the name func_.

0
source share

I would add that this construct, in my experience, is unusual in modern code, but is often found in older code, especially in K & R.

More modern code usually gets the function prototype from the header file.

0
source share

All Articles