Some programmer thinks that defining a function inside an another function is known as “nested function”. But the reality is that it is not a nested function, it is treated as lexical scoping. Lexical scoping is not valid in C because the compiler cant reach/find the correct memory location of the inner function.
Nested function is not supported by C because we cannot define a function within another function in C. We can declare a function inside a function, but it’s not a nested function.
Because nested functions definitions can not access local variables of the surrounding blocks, they can access only global variables of the containing module. This is done so that lookup of global variables doesn’t have to go through the directory. As in C, there are two nested scopes: local and global (and beyond this, built-ins). Therefore, nested functions have only a limited use. If we try to approach nested function in C, then we will get compile time error.
// C program to illustrate the // concept of Nested function. #include <stdio.h> int main(void) { printf("Main"); int fun() { printf("fun"); // defining view() function inside fun() function. int view() { printf("view"); } return 1; } view(); } |
Output:
Compile time error: undefined reference to `view'
An extension of the GNU C Compiler allows the declarations of nested functions. The declarations of nested functions under GCC’s extension need to be prefix/start with the auto keyword.
// C program of nested function // with the help of gcc extension #include <stdio.h> int main(void) { auto int view(); // declare function with auto keyword view(); // calling function printf("Main\n"); int view() { printf("View\n"); return 1; } printf("GEEKS"); return 0; } |
Output:
view Main GEEKS
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- A nested loop puzzle
- Nested printf (printf inside printf) in C
- Nested switch case
- C++ | Nested Ternary Operator
- Nested Loops in C with Examples
- Static functions in C
- Write one line functions for strcat() and strcmp()
- Can static functions be virtual in C++?
- Functions that cannot be overloaded in C++
- Functions that are executed before and after main() in C
- Pure Functions
- Can virtual functions be inlined?
- Macros vs Functions
- fill() and fill_n() functions in C++ STL
- Return from void functions in C++
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Forward List in C++ | Set 2 (Manipulating Functions)
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- exec family of functions in C
- strdup() and strndup() functions in C/C++

