0

I was trying to simultaneously compile the following C files

file1.c

#include<stdio.h>
int main()
{
    foo();
    return 0;
}

file2.c

#include<stdio.h>
void foo()
{
    printf("Hello");
}

I compiled the two files using the following command in linux gcc file1.c file2.c -o file

It compiled successfully without any warnings and on running it gave the output as 'Hello' Shouldn't file1.c require a prototype like void foo(). Is there anything in the C standard regarding this ?

  • What compiler are you using? Pre-C99, it was legal to call a function without a declaration. Without that information, no one can say why you're not getting an error or warning. See https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function (Note for anyone who wants to vote to close: that question is **NOT** a duplicate of this one. This question asks why there was no warning or error - a different question.) – Andrew Henle Aug 04 '19 at 17:33
  • Your compiler is probably too old. You need to upgrade or at the very least increase the default warning level. – n. m. could be an AI Aug 04 '19 at 17:34
  • You want to check your compiler version. GCC has lately defaulted to gnu11 which would automatically warn about this. At least since GCC 5. Warnings can be turned on for it with `-Wall` in any case. – Antti Haapala -- Слава Україні Aug 04 '19 at 17:39

1 Answers1

1

Before C99, C had a thing called implicit declaration that allowed you to do that.

If you didn't specify a declaration for foo and you called foo, it was implicitly declared as int foo();.

This, however, was removed from C99 and subsequent standards.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76