自己实现printf()(转)

my_printf.h:

[csharp] view plaincopy
  1. extern void my_printf(const char *format,...);  


 

my_printf.c:

[csharp] view plaincopy
    1. #include "my_printf.h"  
    2. #include "stdarg.h"  
    3. /********************************************************** 
    4.  
    5. ***********************************************************/  
    6. void printch(const char ch)  
    7. {  
    8.     putchar(ch);  
    9. }  
    10. /********************************************************** 
    11.  
    12. ***********************************************************/  
    13. void printint(const int dec)  
    14. {  
    15.     if(dec == 0)  
    16.     {  
    17.         return;  
    18.     }  
    19.     printint(dec / 10);  
    20.     putchar((char)(dec % 10 + '0'));  
    21. }  
    22. /********************************************************** 
    23.  
    24. ***********************************************************/  
    25. void printstr(const char *ptr)  
    26. {  
    27.     while(*ptr)  
    28.     {  
    29.         putchar(*ptr);  
    30.     ptr++;  
    31.     }  
    32. }  
    33. /********************************************************** 
    34.  
    35. ***********************************************************/  
    36. void printfloat(const float flt)  
    37. {  
    38.     int tmpint = (int)flt;  
    39.     int tmpflt = (int)(100000 * (flt - tmpint));  
    40.     if(tmpflt % 10 >= 5)  
    41.     {  
    42.         tmpflt = tmpflt / 10 + 1;  
    43.     }  
    44.     else  
    45.     {  
    46.         tmpflt = tmpflt / 10;  
    47.     }  
    48.     printint(tmpint);  
    49.     putchar('.');  
    50.     printint(tmpflt);  
    51.   
    52. }  
    53. /********************************************************** 
    54.  
    55. ***********************************************************/  
    56. void my_printf(const char *format,...)  
    57. {  
    58.     va_list ap;  
    59.     va_start(ap,format);  
    60.     while(*format)  
    61.     {  
    62.         if(*format != '%')  
    63.     {  
    64.             putchar(*format);  
    65.         format++;  
    66.     }  
    67.     else  
    68.     {  
    69.         format++;  
    70.         switch(*format)  
    71.         {  
    72.             case 'c':  
    73.             {  
    74.                 char valch = va_arg(ap,int);  
    75.             printch(valch);  
    76.             format++;  
    77.             break;  
    78.             }  
    79.             case 'd':  
    80.             {  
    81.                 int valint = va_arg(ap,int);  
    82.             printint(valint);  
    83.             format++;  
    84.             break;  
    85.             }  
    86.         case 's':  
    87.         {  
    88.             char *valstr = va_arg(ap,char *);  
    89.             printstr(valstr);  
    90.             format++;  
    91.             break;  
    92.         }  
    93.         case 'f':  
    94.         {  
    95.             float valflt = va_arg(ap,double);  
    96.             printfloat(valflt);  
    97.             format++;  
    98.             break;  
    99.         }  
    100.         default:  
    101.         {  
    102.             printch(*format);  
    103.             format++;  
    104.         }  
    105.         }  
    106.         va_end(ap);  
    107.     }  
    108.     }  
    109. }  
    110. /********************************************************** 
    111.  
    112. ***********************************************************/  
    113. int main()  
    114. {  
    115.     char ch = 'A';  
    116.     char *str = "holle world";  
    117.     int dec = 1234;  
    118.     float flt = 1234.3456789;   
    119.     my_printf("str = %s,dec = %d,ch = %c,flt = %f\n",str,dec,ch,flt);  
    120.     return 0;  
    121. }
原文地址:https://www.cnblogs.com/10jschen/p/2599271.html