Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a C Function without Prototype [duplicate]

I have a C file (say file1.c) that calls a function fun1(1,b).

This function fun1(int a,int b) resides in another C file (say file2.c) but its prototype is not included in the header file (say file2.h). file2.h is included in file1.c.

My question is, if I call fun1(a,b) from file1.c, will it work by passing control to the function definition in file2.c? Or will an exception occur or what will be the expected behavior?

Do I have to give a prototype of fun1(int a, int b) in file2.h for this to work?

like image 530
Wilma Avatar asked Feb 23 '23 14:02

Wilma


1 Answers

A couple things can happen depending on the situation and your compiler:

  • You get a compilation error. The compiler throws up its arms and refuses to produce an object file.
  • The compiler treats the function declaration as implied by the call and proceeds to link. It may assume that you know what you're doing in terms of function arguments. Usually it also assumes an int return type. Almost every compiler I've worked with will produce a warning when it does this.
  • The compiler treats the declaration as implied by the call but fails to link. Like the above, but the linker then notices that the implied function you're trying to call and the one you actually did write are different and dies.

You SHOULD provide a prototype regardless.

like image 145
bearda Avatar answered Mar 02 '23 23:03

bearda