I was wondering if there is something wrong with passing a pointer to putchar or any other standard function that can be implemented as a macro of a function that takes a pointer to a function. Below is an example of what I am 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 have no problem compiling this with GCC and Clang under GNU / Linux, as well as GCC under OpenBSD. I am wondering if it will have the same behavior with every other standard compatible implementation, since putchar can be implemented as a macro. I was looking at the standard, especially the function pointer and putchar sections, and could not find anything that indicates whether this is legal or not.
Thanks.
DanielH
source share