Search code examples
cgccimplicit-declaration

Multiple implicit declaration of same function in C


Here's my code:

int main(){
    printf("Hi");
    int i=10;
    printf("Hi %d",i);
    return 0;
}

Now as C can implicitly declare a function this program will compile correctly (As it does with gcc).
But my question is, wasn't the 1st printf declared to return an int with 1 parameter of type char * ?
That makes the 2nd printf an error.
Yet the program compiles with no errors, just warnings (gcc). Why ?


Solution

  • Strictly speaking, implicit declaration is standard violation. It is removed from the standard.

    Quoting C11, Foreword

    Major changes in the second edition included:

    — remove implicit function declaration

    That said, in earlier version of C, for a function which is considered declared implicitly (i.e., used before the compiler has knowledge about the function prototype) was supposed to

    • return an int
    • accepts any number and type of parameter.

    So, as long as the function declaration and the definition does not collide (say, return type mismatch), you'll not get any error. However, a strictly conforming compiler MUST produce a diagnostic.