I think, instead of making your code messy with function pointers, because at the end you cannot specialize the printf function without providing a format, there is no function overload function or template function in C. My suggestion is a special printf function by type.
// print seperator void p (int end) { printf(end?"\n":" "); } // print decimal void pi (long long n) { printf("%lld",n); } // print unsigned void pu (unsigned long long n) { printf("%llu",n); } // print floating point void pf (double n) { printf("%g",n); } // print char void pc (char n) { printf("%c",n); } // print string void ps (char* n) { printf("%s",n); }
Test try here
pi(999),p(0),pf(3.16),p(0),ps("test"),p(1);
Output
999 3.16 test
Another variant
In theory, you can define a polymorphic print function in a structure if you can do something like this. I have not tested this yet.
struct Node { enum NodeType {Long,Double,Char,String} type; union {long l,double d,char c,char* s}; }; void p(Node* n) { switch (n->type) { case Node::NodeType::Long: printf("%ld", n->l); case Node::NodeType::Double: printf("%g",n->d); case Node::NodeType::Char: printf("%c",n->c); case Node::NodeType::String: printf("%s",n->s); } }
source share