1

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.

Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 1
    It doesn't get executed by my compiler (gcc 4.5.2, with `#include ` added and the non-portable `getch` and `clrscr` removed).... What compiler are you using? – Tony Delroy Mar 03 '11 at 07:52
  • @AMIT: that's pretty old isn't it? Probably not Standards compliant. Anyway, the best bet is just to put the function declaration before main()... that's much more common practice anyway. – Tony Delroy Mar 03 '11 at 07:55
  • yaa @Tony its very much old but behaviour is not common and that is what am trying t figure out and when i declared it outside main ,nothing gets exected – Amit Singh Tomar Mar 03 '11 at 07:57
  • 1
    @AMIT: well, it's good that nothing gets executed when it's outside main: I recommend just putting your declaration there then. There's no deep mystery to this - it doesn't work inside main because your compiler's old and broken - just accept that and get on with your work. You can then add an actual call to `show()` (without `void` in front) in main if you want to it to be called from there. – Tony Delroy Mar 03 '11 at 08:01
  • Tony's answer is closer to the truth than any of the other responses. He should post it as an answer so I can stop wasting my rep away downvoting all the misinformation in the answers... – James Greenhalgh Mar 03 '11 at 08:08
  • 1
    I don't understand. You just updated your question and posted your own answer? So what is the question now? Why is Turbo C++ old and broken? Um. Because no one uses it anymore. Time marches on. Use a real compiler. Obviously you have a copy of GCC: use it instead. – Cody Gray - on strike Mar 03 '11 at 08:09
  • yaa i do have GCC but @Cody in india people are really attach to turbo c++ i don't why?? – Amit Singh Tomar Mar 03 '11 at 08:20
  • and one more thing whatever the Explanation: is given looking perfect to me but i don't know why gcc is not following it... – Amit Singh Tomar Mar 03 '11 at 08:22
  • 1
    @AMIT: I don't know why either, but it's pretty unfortunate. There are a lot of smart people in India getting cheated out of a good, relevant education. There's no reason for you to be one of them. I understand not being able to spend money and buy the latest and the greatest, but when it's available for free (GCC, Visual Studio Express, etc.) using the inferior, 20 year old product is just the epitome of silliness. You won't ever be writing 16-bit DOS programs in the real world. Saddling yourself with Turbo C++ means you spend more time learning its quirks than real C. – Cody Gray - on strike Mar 03 '11 at 08:23
  • @AMIT Because they are 20 years behind the rest of the world? – Lundin Mar 03 '11 at 08:26
  • 1
    yaa @Cody you are right ...i should go for latest compiler... – Amit Singh Tomar Mar 03 '11 at 08:57
  • 1
    and i think i just carried away with my question... – Amit Singh Tomar Mar 03 '11 at 08:58

6 Answers6

3

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");
}
Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • @AMIT: *What* compiled well with one warning? You can't ask questions about how to write C code, and then argue that your way is correct because you're using an old, broken, non-standards-compliant compiler. Given the most recent updates to your question, I'm not even sure what you're asking anymore. As you mention, the code you've shown doesn't work in GCC; it's not supposed to. You never *call* the function, you just declare it. There's no reason for the computer to ever execute the code defined inside of your function. – Cody Gray - on strike Mar 03 '11 at 08:15
1

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");
    }
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
1

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().

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • According to AMIT, his compiler did generate a call to show(). Whether it should do so is a different matter. – JayM Mar 03 '11 at 08:10
  • 1
    @JayM He either messed up somehow or he is using a non-C compiler. The question is tagged C, so my reply is related to how a C compiler would behave. – Lundin Mar 03 '11 at 08:20
1

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.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
0

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.

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • yaa @SpyrosP i declared it main() function like void show(); but am not calling it then how does its get executed??? – Amit Singh Tomar Mar 03 '11 at 07:49
  • main is a special C function that describes the beginning of a program. It's the entry point and gets executed by default. – Spyros Mar 03 '11 at 07:59
0

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.

JayM
  • 4,798
  • 1
  • 21
  • 15
  • You _can_ declare functions inside other functions in C (at least gcc accept this), although it is seldom used feature. – CygnusX1 Mar 03 '11 at 07:56
  • but @CygnusX1 could u tell why its taking function declarion as function call any specifuc reason for that??? – Amit Singh Tomar Mar 03 '11 at 08:01
  • -1. It is valid C syntax to declare function prototypes inside other functions, but it is very poor style. See my answer further below. The posted code will *not* execute show(). The compiler does *not* treat the local function declaration as a call. – Lundin Mar 03 '11 at 08:02
  • 2
    The reason your compiler is treating it as a function call is that the compiler is not following the C standard. Either it's a bug or an unusual set of default options to the compiler. – JayM Mar 03 '11 at 08:08