四个参数做测试参数可变的函数

来源:互联网 发布:公司logo免费设计软件 编辑:程序博客网 时间:2024/05/21 16:02
#include <stdio.h>void printf_test(char* fmt,...);//参数可变的函数声明void main(){int a=23,c=99;char b='c';printf_test("%d,%c,%d",a,b,c);//用四个参数做测试}void printf_test(char* fmt,...) //参数可变的函数定义,注意第一个参数为char* fmt{char *p,*p0;//注意不是指向fmt,而是指向&fmt,并且强制转化为char *,以便一个一个字节访问p=p0=(char *)&fmt;p+=4;char *str=fmt;while(*str!='\0'){if(*str!='%'){putchar(*str);str++;}else{str++;switch(*str){case 'd':{printf("%d",(int)(*p) );p+=4;break;}case 'c':{putchar(*p);p+=4;break;}default:{putchar(*str);break;}}//switch(*str)str++;}}putchar('\n');int *pn=(int *)(p0+4);printf("%d\n",*pn);char *pc=(char*)(pn+1);printf("%c\n",*pc);pn=(int *)(pc+4);printf("%d\n",*pn);}//输出/*23,c,9923c99Press any key to continue*/

原创粉丝点击