This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Is this a bug?


It's not a bug. It conforms the C standard. C, unlike C++,
distinguishes functions ONLY by name, not by arguments.

C allows calling functions that are not declared by assuming they
return int. So GCC would assume that the prototype of "func" to be
"int func()" when compiling "main.c", and can generate a call to
"func", although it's defined in another file with a different
prototype. When the prototypes are different, the behavior is
undefined.
(Yes, this would be a link error in C++. But for C, it's the
programmer's responsibility to make sure arguments and return values
are passed correctly if you call undeclared functions. Therefore, it
is always encouraged that every function be declared before called,
even in C code.)


On 9/29/07, Zhang Xiaoping <gcc.compiler@gmail.com> wrote:
> two c files: main.c and func.c, excute the command like this:
>
> gcc main.c func.c -Wall -ansi -pedantic
>
> there are two warnings, and is can generate binary file and the file
> can be excuted.
>
> //main.c
>
> int main()
> {
>     int a;
>     a = func();
>     printf("%d\n", a);
>     return a;
> }
>
> //func.c
> float func(int a, int b)
> {
>     return (float)(a + b);
> }
>
> I assume it's a bug:  func in main funcion is different from the
> function in func.c
>
> My gcc version is gcc 3.2.2.
>
> May be not this version, i have forgotten.
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]