本文由博主原创,转载请注明出处(保留此处和链接):
IT人生(http://blog.csdn.net/ningxialieri/article/details/8981127)
最近在看glib相关的东西,想玩玩可变参,来定义一个自己的printf函数,在此做个小记录:
文件robin_printf.c
$ cat robin_printf.c #include <stdarg.h> #define _robin_vprintf vprintf #define _robin_vsprintf vsprintf int robin_vprintf(char const *format, va_list args){ return _robin_vprintf(format,args); } int robin_vsprintf(char *string, char const * format, va_list args){ return _robin_vsprintf(string,format,args); } int robin_printf(char const *format, ...){ va_list args; int retval; va_start(args,format); retval = robin_vprintf(format,args); va_end(args); return retval; } int robin_sprintf(char *string, char const *format, ...){ va_list args; int retval; va_start(args,format); retval = robin_vsprintf (string, format, args); va_end(args); return retval; }
$ cat main.c #include <stdio.h> #include <stdlib.h> #include "robin_printf.c" int main(){ int i=10; char * buf = malloc(1024); robin_printf("Hello Robin!!!\n"); for(;i>=0;--i){ robin_printf("i=%d\n",i); robin_sprintf(buf,"Robin %d",i); robin_printf("%s\n",buf); } free(buf); return 0; }
文件Makefile
$ cat Makefile SRC=main.c OBJ=main OBJ: gcc -o2 ${SRC} -o ${OBJ} clean: rm ${OBJ}
在此记录一个好博文:http://blog.csdn.net/jxfengzi/article/details/4082891