5

Today i came across nested functions which i had never heard of. Is it only part of GNU C?

Here is a wikipedia example of nested function.

float E(float x)
{
    float F(float y)
    {
        return x + y;
    }
    return F(3);
}

From the code, it looks like nested functions are sort of C++ inline functions. So, is it possible to take out the address of nested function?

Edit:

The gcc link given by Adam says that nested function's code is created dynamically on stack. But how do you run code from stack? Shouldn't it be there in code segment.

chappar
  • 7,275
  • 12
  • 44
  • 57
  • Does this answer your question? [Why are nested functions not supported by the C standard?](https://stackoverflow.com/questions/1348095/why-are-nested-functions-not-supported-by-the-c-standard) – S.S. Anne Nov 10 '19 at 20:38

2 Answers2

13

No, they are not part of the C or C++ standard. They are a GNU extension in the GCC compiler. See the GCC manual for more information. It is actually possible to take the address of a nested function, which is done using a technique called trampolines, but beware of the caveats listed in the manual.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
1

Nested functions are GCC extension, not a standard C

qrdl
  • 34,062
  • 14
  • 56
  • 86