xiaoyi
xiaoyi

Reputation: 6751

Why it's possible to invoke a function declared later in C?

I have this little code to demonstrate it:

#include "stdio.h"

int main() {
    int a = testfunc();  // declared later
    printf("%d", a);
    return 0;
}

int testfunc() {
    return 1;
}

It compiles with no error, and a outputs 1 as expected.

See in action: http://ideone.com/WRF94E

Why there is no error? Is it part of the spec of C or a compiler related thing?

Upvotes: 4

Views: 1339

Answers (6)

Kash
Kash

Reputation: 43

In ANSI C, you do not have to declare a function prototype; however, it is a best practice to use them. If you do not have a prototype, and you call a function, the compiler will infer a prototype from the parameters you pass to the function.The compiler assumes that it has an int return type. If you declare the function later in the same compilation unit with a different return type, you'll get a compile error.

Upvotes: 1

iabdalkader
iabdalkader

Reputation: 17312

The compiler doesn't need to see a function definition to call it, it only needs a prototype to check the arguments, if there isn't any, the compiler will assume the function returns int and that you know what you're doing. However, when linking if the linker can not resolve a symbol only then you will get an error.

Upvotes: 1

Omkant
Omkant

Reputation: 9214

int a = testfunc();

int testfunc() {
    return 1;
}

You are not getting any error because in C default function signature is the same.

If you want the compiler to tell you see the demo below:

call like this without forward declaration and see compiler will give you error.

int a=testfunc(5.4);

int testfunc(float f)
{
 return 1;
}

NOTE : The best practice in C is to give forward declaration of functions which you are going to use it in program.

Upvotes: 1

Israel Unterman
Israel Unterman

Reputation: 13510

This is compiler dependant. Some compilers will emit an error, others - a warning, while others - nothing. The compiler duty is only to check that you use the function with the correct arguments. If you don't declare it, then the compiler can't check and warn you for misuse of the function, but it can generate the code for calling the function. The linker duty, on the other hand, is to see that the function is implemented and put its address in the call generated previously by the compiler. Since your function is implemented, the linker works fine.

Since your compiler doesn't check you, you can try calling your function with arguments. The call might work but might also make your program crash.

Upvotes: 1

Razvan
Razvan

Reputation: 10093

The function testfunc() is implicitly declared. The compiler cannot do any signature checks so you might get a runtime error in case you fail to invoke it corectly.

This is part of the C specification. But the recommendation in the C specification is to declare all the functions you plan to implement and use, at the beginning of the current file or in a header file.

Upvotes: 5

Jeyaram
Jeyaram

Reputation: 9484

Because, by default the compiler considers the undeclared functions as

int function_name ();

Internally, an undeclared function is considered with return type as int and accepts any number of arguments.It matches with your actual function also. so no issue.

Upvotes: 2

Related Questions