Why doesn't C have nested functions?

Showing Answers 1 - 12 of 12 Answers

hariharan

  • Aug 13th, 2005
 

we does not have nested functions in c because we use concept of friend function 

  Was this answer useful?  Yes

satish

  • Aug 26th, 2005
 

We don't have anything like friend function in C it is in C++

  Was this answer useful?  Yes

praveena

  • Jul 31st, 2006
 

since it has friend function it doesnt have nested function

  Was this answer useful?  Yes

s.vamsikrishna

  • Nov 17th, 2006
 

c does not have nested functions because c doesnot not support encapsulation.in c each and every function is independent to each other and performs its assigned task separately

  Was this answer useful?  Yes

priti

  • Jan 18th, 2007
 

#include<stdio.h>
int add(int a,int b)
{
    int c=3;
    int d=a+b;
    int addToC(int c, int d )
    {
        printf("This is addToC.....n");
        return(c+d);
    }
    return(addToC(c,d));
}
int main()
{
    printf(" Addition   %d n" ,add(1,2));
    return 0;
}

govardhan

  • Apr 20th, 2007
 

It's not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were deliberately left out of C as a simplification. (gcc does allow them, as an extension.) For many potential uses of nested functions (e.g. qsort comparison functions), an adequate if slightly cumbersome solution is to use an adjacent function with static declaration, communicating if necessary via a few static variables. (A cleaner solution, though unsupported by qsort, is to pass around a pointer to a structure containing the necessary context.)

 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions