English 中文(简体)
prior declarations of functions
原标题:

Why do functions need to be prior declared in C ?

最佳回答

Functions in modern C language need to be prior-declared for two reasons: 1) to tell the compiler that a specific name is the name of a function (and not of something else), 2) to tell the compiler the exact return type of the function so that the compiler can handle that return correctly.

In C a function can be declared with or without prototype. A prototype declaration provides more information to the compiler. It includes information about the number and the types of function parameters (if any), thus helping the compiler to prepare the arguments for the function call properly.

In C89/90 version of C language the functions didn t have to be prior declared, which resulted in the compiler making assumptions about the function at the point of the call. Needless to say, in many cases this proved to be dangerous.

问题回答

So that the compiler will be able to detect type errors when you call functions. Of course there are ways around that, but that s the way they chose.

So that the one-pass compiler knows how many bytes to pass for each argument.

Consider:

f(12345);

int f(char input)
{
   printf("%c",input);
}

Without a prototype, the compiler will assume that f accepts ints, and send sizeof(int) bytes to the function (through stack or registers, depending on the platform). But the function will only look at 1 byte, which will give the wrong result.

Because old compilers have limited speed and memory, so they want everything can be done in one pass (just read the file from top to bottom and everything is understood).

Modern-designed compilers can look for functions up, down, even in different files when it is not yet declared at a point.

Basically, you don t....

A lot of compilers will assume you re calling a int Function() signature, if you haven t declared it yet. The linker... hmmm.. will probably not eat that.

The compiler will mind if the signature of the declared Functions doesn t match the calling statement, but it is at a second step where things go wrong: - coding for argument passing and return value pre-code

The latter actually has to be determined for each calling. It s at this second code-generation step that C compiler really misses the declaration (which is what makes a function signature). Then the linker also needs to turn symbolic calls of functions into actual... er... calls. But if the function exists "somewhere" (go investigate the extern modifier), the linker won t be a show stopper.

The exception to all this is the function pointer mechanism, where you tell the compiler and linker the signature to be expected, but the call itself is not decided by the compiler , neither you have a "hardcoded" call by the linker... check it out.

I am sure this is not the only reason, but you can compile files with calls to other functions when you only know the declaration of the function for example from a header file. This is possible without recompiling the definition of the function itself (which might be in another file). But to verify the function is called correctly, the compiler has to know its declaration. Then the linker will take care of the rest.

Here a little example

main.c:

#include "function.h"

int main(){
    function();
    return 0;
}

function.h:

#ifndef FUNCTION_H
#define FUNCTION_H

void function();

#endif

function.c:

#include "function.h"

void function(){}

I am using gcc to compile like that:

gcc function.c -c 

this will produce an object file function.o. Now, when I want to compile my main function, I don t have to compile my function.c file anymore, I only have to know the declaration from the header file and the object file:

gcc main.c function.o -o test

Now, the object file will get linked into my program without recompiling.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签