Can we have a nested function in C? What is the use of nested functions? If they exist in C does there implementation differes from compiler to compiler.
Are nested functions allowed in any other language? If yes then what is there significance?
Can we have a nested function in C? What is the use of nested functions? If they exist in C does there implementation differes from compiler to compiler.
Are nested functions allowed in any other language? If yes then what is there significance?
No, they don't exist in C.
They are used in languages like Pascal for (at least) two reasons:
You cannot define a function within another function in standard C.
You can declare a function inside of a function, but it's not a nested function.
gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.
No you can't have a nested function in C
. The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.
E.g.
void f(void)
{
// Declare a function called g
void g(void);
// Call g
g();
}
// Definition of g
void g(void)
{
}
Nested functions are not a part of ANSI C, however, they are part of Gnu C.
As others have answered, standard C does not support nested functions.
Nested functions are used in some languages to enclose multiple functions and variables into a container (the outer function) so that the individual functions (excluding the outer function) and variables are not seen from outside.
In C, this can be done by putting such functions in a separate source file. Define the main function as global and all the other functions and variables as static. Now only the main function is visible outside this module.