Interview Questions

My compiler is complaining about an invalid redeclaration of a function

C Interview Questions and Answers


(Continued from previous question...)

My compiler is complaining about an invalid redeclaration of a function

Q: My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once.

A: Functions which are called without a declaration in scope, perhaps because the first call precedes the function's definition, are assumed to be declared as if by:
extern int f();

That is, an undeclared function is assumed to return int, and to accept an unspecified number of arguments (though there must be a fixed number of them and none may be ``narrow''). If the function is later defined otherwise, the compiler complains about the discrepancy. Functions returning other than int, or accepting any ``narrow'' arguments, or accepting a variable number of arguments, must all be declared before they are called. (And it's by all means safest to declare all functions, so that function prototypes can check that arguments are passed correctly.)

Another possible source of this problem is that the function has the same name as another one declared in some header file.

(Continued on next question...)

Other Interview Questions