Nested function in C

2019-01-02 18:18发布

Can we have a nested function in C? What is the use of nested functions? If they exist in C does their implementation differ from compiler to compiler?

Are nested functions allowed in any other language? If yes, then what is their significance?

8条回答
忆尘夕之涩
2楼-- · 2019-01-02 18:42

is this not a nested function in C? ( the function displayAccounts() )

I know I could have defined the function differently and passed variables and what not but anyhow works nicely as I needed to print the accounts multiple times.

(snipet taken from a school assignment)...

//function 'main' that executes the program.
int main(void)
{
    int customerArray[3][3] = {{1, 1000, 600}, {2, 5000, 2500}, {3, 10000, 2000}};  //multidimensional customer data array.
    int x, y;      //counters for the multidimensional customer array.
    char inquiry;  //variable used to store input from user ('y' or 'n' response on whether or not a recession is present).

    //function 'displayAccounts' displays the current status of accounts when called.
    void displayAccounts(void)
    {
        puts("\t\tBank Of Despair\n\nCustomer List:\n--------------");
        puts("Account #    Credit Limit\t  Balance\n---------    ------------\t  -------");
        for(x = 0; x <= 2; x++)
        {
            for(y = 0; y <= 2; y++)
                printf("%9d\t", customerArray[x][y]);
            puts("\n");
        }
    }

    displayAccounts();  //prints accounts to console.
    printf("Is there currently a recession (y or n)? ");


//...

    return 0;
}
查看更多
低头抚发
3楼-- · 2019-01-02 18:48

Nested functions are not a part of ANSI C, however, they are part of Gnu C.

查看更多
登录 后发表回答