declare a function in main?

Some example i run into for a program that deals with menu..

He declared all the function before the main function as i understand should be, and then one of the function that is a void function was also mentioned inside the main:

char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
    int choice;
    void count(void);
    while ( (choice = get_choice()) != 'q')
    {
        switch (choice)
        {
            case 'a' : printf("Buy low, sell high.n");
                break;
            case 'b' : putchar('a'); /* ANSI */
                break;
            case 'c' : count();
                break;
            default : printf("Program error!n");
                break;
        }
    }
    printf("Bye.n");

...(functions implementations)

Can you please tell me why is that? tnx


These are just declaration of the functions not definitions.Not too sure why count function is declared twice though.The declaration is just saying to the compiler that there is something there with this name.Perhaps the programmer forgot to define the method?

A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol--if it's a function, what it does; if it's a class, what fields and methods it has; if it's a variable, where that variable is stored.

eg declaration looks like this:

void count(void);

eg definition looks like this:

void count(void){

......

}

根本没有理由,这只是对原型的毫无意义的重复。


Doesn't matter - wherever makes sense for your program. Obviously, if it's inside the main, then no other function before the actual function implementation will "know" what the function prototype is, which can have an impact.

I personally tend to implement the function before it is called, that way avoiding the problem of where to put the prototype [unless it goes in a headerfile, in which case that tends to solve the problem].

链接地址: http://www.djcxy.com/p/72184.html

上一篇: 主要功能中的论点点

下一篇: 在main中声明一个函数?