andreyvul <> writes:
> Suppose I have code like this:
> #include <stdio.h>
> int printf(const char *fmt, ...) {
> puts("Hello, world\n");
> }
>
> When compiling, I get an error that printf was previously defined in
> stdio.h (and it's correct).
No, it was previously *declared* in stdio.h.
> However, there are a lot of things that I need in stdio.h so I can't
> uninclude it.
> How do I tell the compiler to use the new printf without removing the
> #include?
As far as the C standard is concerned, you can't redeclare a standard
function; any attempt to do so invokes undefined behavior. For
example, the compiler is free to assume that any call to a function
named "printf" is really a call to the standard one; one popular
compiler can replace this:
printf("Hello, world\n");
with this:
puts("Hello, world");
Your implementation *might* let you do this somehow. Check your
compiler's documentation.
But there's another way: use a macro:
#include <stdio.h>
int my_printf(const char *fmt, ...) {
puts("Hello, world\n");
}
#define printf my_printf
int main(void) {
printf("Good-bye, cruel world!\n");
return 0;
}
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"