开发者

Nested functions in C?

can any one tell me can we declare a function in user defined function ? such as

int sum(int x, int y)
{
    int fun( float x);
}

Can we define a function inside a function ? As I know we can't define a function inside a function.

But I just did this and its work fine the code is given below :

{
    int main()
    { 
        func1();
        return 0;
    }
    func1()
    {
        int i = 0;
        auto func2()
        {
            i = 10;
            printf("Heloo i am func 2\n");
        }
        printf("Heloo i am func 1\n");
        func2();
    }
}

It works very fine .

Now can anyone tell me what is going around how a function inside a function is defined or working properly ?

Can anybody explain to me why the code is working ?

Now when I changed few lines of code it give me following problems The changes are following:

code:

{
    func1()
    {
        func2();
        int i = 0;
        auto func2()
        {
            i = 10;
            printf("Heloo i am func 2\n");
        }
        printf("Heloo i am func 1\n");
    }

error:

error: static declaration of ‘func2’ follows non-static declaration
note: previous implicit declaration of ‘func2’ was here

Now what are these error and why are they coming ?

If I call fun开发者_运维百科c2() in main function than it will show an error like undefined reference to func2

Now can anyone tell me what is wrong here ?


The C standard allows the declaration of functions within functions (as in your first code snippet), but not the definition of functions within functions (although some compilers may offer it as a non-standard extension).

The same is true for C++, although newer versions (C++0x, etc.) allow you to define anonymous lambda functions. But this is something different.


gcc allows this in C only (not C++) via -fnested-functions, but this is of course non-standard and non-portable so you should probably not use nested functions unless you have a very good reason.


This, for foo,

int main(void) {
    int foo(void);
}

is not a definition of foo. It is a declaration and it is perfectly valid (though strange: function declarations belong outside any other functions) in C.

What C does not allow, which is what you found out, is that definitions cannot be nested.

/* INAVLID EXAMPLE */
int main(void) {
    int foo(void) { return 0; }
}


When you call func2() before declaration, C assumes (according to standard) implicit declaration int func2(...), but when compiler finds func2() definition, it differs from previous implicit declaration, so compiler complains about it. If func2() precedes the call to func2(), it acts as both definition and declaration, so no problem there.

And of course func2() cannot be called from main() as its scope is limited to func1(), like you cannot access local variable from another function.

As others mentioned, nested functions aren't standard in C, although some compilers (like GCC) support this feature.


This is just a supplement to Paul R's answer. Assuming that your compiler is GCC, the end of the document in Paul's answer says:

A nested function always has no linkage. Declaring one with extern or static is erroneous. If you need to declare the nested function before its definition, use auto

So func2 has to be declared as auto before the call of it.
If you change the second code

func1()
{
    func2();
    int i = 0;
    ...

to

func1()
{
    auto func2(); /* declaration added */
    func2();
    int i = 0;
    ...

the code will be compiled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜