ZoomIn
ZoomIn

Reputation: 1321

Is it portable to define and declare the function after calling it in main in C?

I came across the following difference related to function definition/declaration between C and C++.

In C++ if I call a function before declaring and defining it,I get the error message 'function name is invalid identifier' which is fine for me as it sounds reasonable.

In C with Visual Studio compiler when I compiled the following program I get the error message:

error C2371: 'fun' : redefinition; different basic types

and in gcc-4.3.4 it executed successfully with just this warning:

warning: conflicting types for ‘fun’

Here goes the program:

#include <stdio.h>
int main(){
    fun();
    return 0;
}
void fun(){
    printf("It is fun");
}

So is it fine in C to just call a function and later bother about defining it?! And why the compilers behave differently?

Upvotes: 2

Views: 3048

Answers (6)

Jens Gustedt
Jens Gustedt

Reputation: 78943

Your code is invalid for C++ (as others said already) and also for C99. None of them allows implicit declaration of functions.

There is not much reason to allow such a thing, it is not very difficult to maintain a header file that contains the prototype of the functions that you use.

Upvotes: 1

Baltasarq
Baltasarq

Reputation: 12212

The problem is that the declaration of fun() and its implementation differ in its return type:

fun()   /* int return type assumed */

/* ... */

void fun() { /* ... */ }   /* void "return type" */

However, in the standard C++ ISO 98 it was stated that the compiler should not assume int type for missing types anymore (not sure about C'99, while you post your question for C AND C++).

Finally, declaring a function inside main() is possible, but not a good common practice, unless the function fun() should not be known to the rest of the program.

Upvotes: 0

Prasoon Saurav
Prasoon Saurav

Reputation: 92874

C supports 'implicit int' return type i.e fun(); would be treated as a declaration of a function fun taking no arguments and returning an int. This is because you have not declared the function before.

This is not the case in C++ wherein your code is ill-formed.

Upvotes: 1

wolfgang
wolfgang

Reputation: 4953

When you call the function without a prototype in C89 (disclaimer: I don't know about C99), you are implicitly declaring it as

int fun();

Note that in C, other than in C++, the empty parantheses just mean that you haven't specified what arguments it takes, not that it takes zero arguments.

When you redeclare the function as returning void, you get the warning, but no error. If you return something non-trivial (structs, floats, ...) instead of void or if you try to use the int result of the function, then you might be in deep trouble at runtime.

Upvotes: 2

fge
fge

Reputation: 121780

You must declare the function before. If it is not declared, by default the function is supposed to return int and can take any number of arguments.

Upvotes: 2

sharptooth
sharptooth

Reputation: 170509

This will only reliably work in C when the function has no parameters and returns int so yes, it is portable, but a very bad practice - here's a very good example explaining why.

Upvotes: 1

Related Questions