Consider the next piece of code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int printf(const char *format, ...)
{
va_list args;
return 1;
}
int main()
{
printf("Hello there");
return 0;
}
And indeed, printf prints nothing. Meaning, the compiler 'prefer' this function over the one declared in <stdio.h>. But C doesn't allow function overriding, and from my understanding, it should throw an error of conflicting errors.
Can anyone explain this behavior?
The C standard says (7.1.3 Reserved identifiers):
So when <stdio.h> is included, printf is reserved as an identifier with file scope in the name space of ordinary identifiers. In addition, printf is always reserved as an identifier with external linkage, even if <stdio.h> is not included.
7.1.4 Use of library functions does not allow to define a function with a reserved name.
In most implementations such definition will silently replace the library function, but the standard does not guarantee anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With