I was wondering whether there is anything wrong with passing a pointer to putchar, or any other standard function which can be implemented as a macro, to a function accepting a function pointer. Below is an example of what I’m doing.
#include <stdio.h>
static int print(const char *s, int (*printc)(int))
{
int c;
while (*s != '\0') {
if ((c = printc(*s++)) < 0)
return c;
}
return printc('\n');
}
int main(void)
{
print("Hello, world", putchar);
return 0;
}
I don’t have any problems compiling this with GCC and Clang under GNU/Linux, as well as GCC under OpenBSD. I’m wondering whether it will have the same behaviour under every other standard compliant implementation, since putchar can be implemented as a macro. I’ve searched through the standard, particularly the sections on function pointers and putchar, and haven’t been able to find anything that specifies whether this is legal or not.
Thanks.
Following on from my comments-based discussion with @pmg, I found the relevant section of the standard (C99, 7.1.4 p.1):