Less code, confusing boundaries and starting

Source: Internet
Author: User

1      for(p = fmt; *p; p++)2     {3         if(*p! ='%')4         {5Putchar (*p);6             Continue;7         }8i =0;//always point to the next character, since 0 o'clock also has no characters, and may end up with no characters9localfmt[i++]='%';//start local FMTTen          while(* (p+1) &&!isalpha (* (p+1)) )//p is only processed to the current character test next character ++p test p+1 One                                             //I process to the next character starting from 0 add using i++ Alocalfmt[i++]=* (++P);//Collect chars -localfmt[i++]=* (p+1);//format Letter -localfmt[i]=' /'; the              -         Switch(*++p)//format Letter -         { -  +         } -}
#include <stdio.h>#include<ctype.h>#include<stdarg.h>voidminprintf (Char*FMT, ...)    {va_list ap; Char*p, *Sval; intIval,i; DoubleDval; Charlocalfmt[ -];    Va_start (AP, FMT);  for(p = fmt; *p; p++)    {        if(*p! ='%') {Putchar (*p); Continue; } I=0; Localfmt[i++]='%';  while(* (p+1) &&!isalpha (* (p+1)) Localfmt[i++]=*(++Q); Localfmt[i++]=* (p+1); Localfmt[i]=' /'; Switch(*++p) { Case 'D': Ival= Va_arg (AP,int);                printf (localfmt,ival);  Break;  Case 'F': Dval= Va_arg (AP,Double);                printf (localfmt,dval);  Break;  Case 's':                 for(Sval = Va_arg (AP,Char*); *sval; sval++) Putchar (*sval);  Break; default: Putchar (*p);  Break; }} va_end (AP);}intMain () {minprintf ("% d",Ten); return 0;}
Full Code

For the start of the use of the number of i++ always point to the location to write (the next position of the character), because there may be nothing to write, and finally processed to the empty position I.

For existing tests, use the next character (P+1), and then first move the pointer over the current character, ++p.

Less code, confusing boundaries and starting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.