Reputation: 18201
I have simple programm in two units:
count_words.c:
int main( int argc, char ** argv )
{
printf("starting\n");
int i = aaa(55555);
printf("%d",i);
printf("ending\n");
return i;
}
clean.c:
int aaa()
{
printf("aaa\n");
return 5;
}
Makefile:
count_words: clean.o count_words.o -lfl
gcc count_words.o clean.o -lfl -ocount_words
%.o:%.c
gcc -c -o $@ $<
I'm wondering why this code compiles and runs without problem. Function aaa()
in clean.c
has no parameters, but in count_words.c
I pass 55555
. Why it compiles and runs. Can I expect unexpected errors in such situations?
UPD:
I have changed int aaa()
to int aaa(void)
, but still have warning and not error.
As you noticed I didn't include header of clean.c
in count_words.c
and it compiles anyway. Why then I must include headers at all?
Upvotes: 0
Views: 315
Reputation: 311068
The program has undefined behaviour.
According to the C Standard (6.5.2.2 Function calls)
- ..If the number of arguments does not equal the number of parameters, the behavior is undefined.
The function is defined as having no parameters but is called with one argument.
From the C Standard (6.7.6.3 Function declarators (including prototypes))
14....An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
In C++ this code will not compile.
Upvotes: 1