简体   繁体   中英

Function declaration in C

I am learing C and would like to know is it possible to replace function call with function declaration ..like in below programme

main() {
    void show();
    getch();
}

void show() {
    clrscr();
    printf("Tendulkar is best batsman ever");
}

here in main am declaraing show function and not calling it anywhere but still

printf("Tendulkar is best batsman ever");

is getting executed.why is it so??

And one more thing guys when i run below programme on turbo c++ is giving me error but on gcc its work fine

main()
{
show();

}
void show()
{

printf("Tendulkar is best batsman ever");
}

expected

Answer:

Compier error: Type mismatch in redeclaration of show.

Explanation:

When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.

The program you've shown doesn't compile. Neither me nor my compiler knows what clrscr() is, and if I remember correctly, getch() is an old MS-DOS function defined in conio.h , which hasn't been included with a C compiler for at least 20 years. If you're trying to learn C from a book, throw it away and get a new one. You'll find a suggested list of books here .

Even once that's addressed and all of the undefined functions are removed, we end up with the following little gem of a program:

int main()
{
    void show();

    return 0;
}

void show()
{
    printf("Tendulkar is best batsman ever");
}

which compiles just fine, but doesn't do anything. See for yourself . The program doesn't output anything.

You're supposed to define functions before you use them, and calling a function is not the same thing as defining it. You're not allowed to mix the two up, and if you do, your program reserves the right not to work as expected. You can define a function within a method in C (although it's considered very poor style), but this does not call the method. Function calls do not include the return type. Again, any good book on C programming would tell you this much better than a few answers on Stack Overflow could.

Just for comparison, a correctly written sample would look something like this:

#include <stdio.h>

void show();    // forward declaration of the show function

int main()
{
    show();     // call the show function
    return 0;
}

void show()     // definition of the show function
{
    printf("Tendulkar is best batsman ever");
}

You need to put your functions declarations outside your main method.

    void  show(void);

    main()
    {
    getch();
    }

    void show()
    {
    clrscr();
    printf("Tendulkar is best batsman ever");
    }

You are declaring a function inside the definition of another function, which is valid C but very poor style.

show() will not be executed unless you call it. The code posted will not execute show().

Are you certain you actually succeed with compilation of your code?

You declare a function void show(void) , but you define void show(int) . This code, even if compiles, should just use getch and exit.

I suspect that you are running a different program, probably an output from your previous succesful compilation.

A function in C has to be declared before it can be executed. Executing it and declaring it are two different things than cannot be replaced. Printf is an internal c standard library function that is already declared for you. But for your own functions, you would have to declare everything before executing it.

You don't declare functions from within other functions in C. That's what you are trying to do, but the compiler treats your void show(); as a call to show(). Move your declaration outside of main() and it will behave like you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM