淘先锋技术网

首页 1 2 3 4 5 6 7
#include <stdio.h>

#ifndef round
	#define round(type) \
		((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 
#endif 
typedef char*  var_list;

#ifndef _var_start
#define _address(v) (&(v))
#define _var_start(ap,v) (ap = ((var_list)_address(v) + round(v)))
#define _var_arg(ap,t) (*(t*)((ap += round(t)) - round(t)))
#define _var_end(ap) (ap = NULL)
#endif

#ifndef var_start
#define var_start _var_start
#define var_end   _var_end
#define var_arg   _var_arg
#endif

void print(char* format,...){
	var_list ap;
	char* p = NULL;
	var_start(ap, format);
	for (p = format; *p; ++p){
		if (*p != '%'){
			putchar(*p);
			continue;
		}
		switch (*(++p)){
		case 'd':
			printf("%d", var_arg(ap, int));
			break;
		case 'f':
			printf("%f", var_arg(ap, double));
			break;
		case 's':
			printf("%s", var_arg(ap, char*));
			break;
		default:
			break;
		}
	}
	var_end(ap);
}



int main(int argc, char* argv[])
{
	print("%f hello world!", (float)5);
	getchar();
	return 0;
}