简体   繁体   中英

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 ?

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 (ie, 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.

在您的情况下, printf被隐式定义为int printf(...) ,而不是int printf(char *) ,因此,当您使用其他参数调用编译器时,编译器不会检测到错误。

Your code is not portable C since you're missing the requisite #include which brings in the function prototype for printf : any sort of implicit declaration was removed in C99.

If you want to write non-portable C, then the best thing you can do is to consult your compiler documentation. In this instance it looks like your friendly compiler is defaulting to int printf(...) .

gcc unfortunately has some built in notion of printf even though a header file has not been used.

unsigned int fun ( unsigned int x )
{
    printf("%s\n");
    printf("%u\n",x);
    more_fun("%s\n");
    more_fun("%u\n",x);
    return(x+1);
}

so.c: In function ‘fun’:
so.c:5:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
     printf("%s\n");
     ^
so.c:5:5: warning: incompatible implicit declaration of built-in function ‘printf’
so.c:5:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
so.c:5:12: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
     printf("%s\n");
            ^
so.c:7:5: warning: implicit declaration of function ‘more_fun’ [-Wimplicit-function-declaration]
     more_fun("%s\n");
     ^

fortunately if we make our own it does forget at least a little.

void printf(char *, unsigned int );
void more_fun(char *, unsigned int );
unsigned int fun ( unsigned int x )
{
    printf("%s\n");
    printf("%u\n",x);
    more_fun("%s\n");
    more_fun("%u\n",x);
    return(x+1);
}

so.c:3:6: warning: conflicting types for built-in function ‘printf’
 void printf(char *, unsigned int );
      ^
so.c: In function ‘fun’:
so.c:7:5: error: too few arguments to function ‘printf’
     printf("%s\n");
     ^
so.c:3:6: note: declared here
 void printf(char *, unsigned int );
      ^
so.c:9:5: error: too few arguments to function ‘more_fun’
     more_fun("%s\n");
     ^
so.c:4:6: note: declared here
 void more_fun(char *, unsigned int );
      ^

but we are talking about one compiler, one compiler does not cover it you have to try many/all. Standard or not a compiler could still notice your changing the use of the function and let you know about it, this compiler chooses not to. The error here is not that the compiler didnt notice the function being used differently from one undeclared instance to another but that there was no declaration, and then it does notice the difference as one would hope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM