Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why re-declare a function in main?

Tags:

c

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

like image 347
MNY Avatar asked Jan 31 '13 16:01

MNY


2 Answers

No reason at all, this is just a pointless repetition of the prototype.

like image 167
unwind Avatar answered Nov 03 '22 13:11

unwind


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){

......

}
like image 43
Brian Var Avatar answered Nov 03 '22 12:11

Brian Var