DOS系统下利用TC编译器实现 c语言printf()函数

下面是不定参数的基本知识 

 

 

以下是简化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++;
	}

  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值