下面是不定参数的基本知识
以下是简化printf()函数的实现代码
void prin(char *, ...);
main()
{
prin("hello world!%d,%c,%d",200,'b',1000);
}
void prin(char *s,...)
{
int h=10,l=40,bpcnt=0;
char *p=s;
while(*p!=0)
{
if(*p=='%')
{
if(*(p+1)=='c')
{
*(char far*)(0xb8000000+h*160+l*2)=*(int *)(_BP+6+bpcnt*2);
l++;
bpcnt++;
p = p + 2;
}
else if(*(p+1)=='d')
{
int temp=*(int *)(_BP+6+bpcnt*2);
int strcnt=0,strcnt1=0;
while(temp!=0)
{
temp=temp/10;
if(temp==0)break;
strcnt++;
}
strcnt1=strcnt;
temp=*(int *)(_BP+6+bpcnt*2);
do
{
*(char far*)(0xb8000000+h*160+(l+strcnt)*2)= temp%10+48;
temp=temp/10;
}while(strcnt--);
l=l+strcnt1+1;
bpcnt++;
p = p + 2;
}
else
{
*(char far*)(0xb8000000 + h * 160 +l * 2) = '%';
l++;
p++;
}
}
else
{
*(char far*)(0xb8000000 + h * 160 + l * 2) = *p;
l++;
p++;
}
}
}