源代碼 關於printf

<日 現在才發現 我們通常用的printf並不是這個printf 找文件找錯了,因爲下面的printf 沒有%f之類的輸出  處理>

在裏面發現宏   #define do_div(n,base) 展開後 奇怪的代碼測試如下:

 #include <stdio.h>
  2 
  3 int main()
  4 {
  5 char tmp[66];
  6 int num;
  7  static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQR    STUVWXYZ"; */
  8 int base =10;
  9 
 10 scanf("%d",&num);
 11 
 12 int i=0;
 13 
 14 while(num != 0)
 15 {
 16      tmp[i++] = (
 17      digits[
 18      (
 19      {
 20      int __res;
 21      __res = ((unsigned long) num) % (unsigned) base;
 22      num = ((unsigned long) n) / (unsigned) base;
 23      __res;
 24      }
 25      )//這樣寫也行 還是我的gcc兼容C99的標準?待會兒回到VC下測試 再寫結果,測試過了,這是c99的標準, 在c89下要將分號改成逗號表達式 即使是c++也不能編譯通過
 26      ] | 0)
 27      ;
 28 }
     tmp[i]='\0';
 29  printf("%s\n",tmp); 
 30 return 0;
 31 }

/**/



#include <stdio.h>
#include <stdarg.h>
//extern  int myvsprintf(char *buf, const char *fmt, va_list args);
//#include "myprintf.c"
#include <stdio.h>
 
 #define ZEROPAD 1               /* pad with zero */
  #define SIGN    2               /* unsigned/signed long */
  #define PLUS    4               /* show plus */
  #define SPACE   8               /* space if plus */
  #define LEFT    16              /* left justified */
  #define SMALL   32              /* Must be 32 == 0x20 */
  #define SPECIAL 64              /* 0x */

  #define do_div(n,base) ({ \
  int __res; \
  __res = ((unsigned long) n) % (unsigned) base; \
  n = ((unsigned long) n) / (unsigned) base; \
  __res; })
/*宏函數 : */
static char *number(char *str, long num, int base, int size, int precision,
                     int type)
 {/*str 原指向 緩衝數組 調用處 爲 str = number(.....) 自己將自己改變了 */    /*爲%d時  num=2, base=10, size=-1, precision=-1(精度), type=2(???))*/
         /* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
         static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
 
         char tmp[66];
         char c, sign, locase;
         int i;
 
         /* locase = 0 or 0x20. ORing digits or letters with 'locase'
          * produces same digits or (maybe lowercased) letters */
         locase = (type & SMALL);
         if (type & LEFT)
                 type &= ~ZEROPAD;
         if (base < 2 || base > 36)
                 return NULL;
         c = (type & ZEROPAD) ? '0' : ' ';/*d的時候2&2 取後面' '??單獨測試應該取前面 這是怎麼回事?*/
         sign = 0;
         if (type & SIGN) {/*爲正數 檢測下面 但是不走下面*/
                 if (num < 0) {
                         sign = '-';
                         num = -num;
                         size--;
                 } else if (type & PLUS) {
                         sign = '+';
                         size--;
                 } else if (type & SPACE) {
                         sign = ' ';
                         size--;
                 }
         }
         if (type & SPECIAL) {
                 if (base == 16)/*多少進制*/
                         size -= 2;
                 else if (base == 8)
                         size--;
         }
         i = 0;
         if (num == 0)
                 tmp[i++] = '0';
         else
                 while (num != 0)
                         tmp[i++] = (digits[do_div(num, base)] | locase);/*tmp 是字符串數組 *//*假如 num = 1234 則算後tmp[] 爲4321*/
         if (i > precision)/*注意: i 會隨着上面的i而增加*/
                 precision = i;
         size -= precision;/*精度 減去?????*/
         if (!(type & (ZEROPAD + LEFT)))/*%d 默認會走這裏*/
                 while (size-- > 0)
                         *str++ = ' ';/*精度 控制加空格 補齊*/
         if (sign)/*默認不走這 默認sign = 0*/
                 *str++ = sign;
         if (type & SPECIAL) {/*%d默認不走這*/
                 if (base == 8)
                         *str++ = '0';//原來是空 自己改成0
                 else if (base == 16) {
                         *str++ = '0';//原來是空 自己改成0
                         *str++ = ('X' | locase);
                 }
         }
         if (!(type & LEFT))/*默認走這裏*/
                 while (size-- > 0)
                         *str++ = c;
         while (i < precision--)
                 *str++ = '0';//原來是空 自己改成0
	 while (i-- > 0)/*i 表示數字轉換後的大小長度 假如123則長3*/
                 *str++ = tmp[i];/*正確順序放入*/
         while (size-- > 0)/*d默認的size怎麼爲-4??*/
                *str++ = ' ';
        return str;
 }





static inline int isdigit(int ch)/*檢測是否是數字字符*/
{
         return (ch >= '0') && (ch <= '9');
}

static int skip_atoi(const char **s)
 {
         int i = 0;
  
        while (isdigit(**s))
                 i = i * 10 + *((*s)++) - '0';
         return i;
  }

int myvsprintf(char *buf, const char *fmt, va_list args)
/* 將要打印的值放入buf中 fmt是格式化, *args是原來第一個參數的地址(第0個參數的位置是原來的 &fmt(不是這裏的&fmt))    */

  {
         int len;
         unsigned long num;
         int i, base;
         char *str;
         const char *s; 
         int flags;              /* flags to number() *//*????*/
	 int field_width;    /*width of output field*/
         int precision;          /* min. # of digits for integers; max
                                    number of chars for from string */
         int qualifier;          /* 'h', 'l', or 'L' for integer fields */
 
         for (str = buf; *fmt; ++fmt) {
                 if (*fmt != '%') {/*比如 \n 之類的 直接存放進去*/
                         *str++ = *fmt;
                         continue;
                 }
 
                 /* process flags */
                 flags = 0;//??????????
               repeat://?????????
                 ++fmt;          /* this also skips first '%' */
                 switch (*fmt) {/*不是判斷%d或者%f之類的檢測*//*??????????????與下面的+- 0有關*/
                 case '-':/*?????????????????*/
                         flags |= LEFT;
                         goto repeat;
                 case '+':
                         flags |= PLUS;
                         goto repeat;
                 case ' ':
                         flags |= SPACE;
                         goto repeat;
                 case '#':
                         flags |= SPECIAL;
                         goto repeat;
                 case '0':
                         flags |= ZEROPAD;
                         goto repeat;
                 }
 
                 
		 
		 
		 /* get field width */
                 field_width = -1;
                 if (isdigit(*fmt))/*檢測是不是數字 以便寬度輸出*/
                         field_width = skip_atoi(&fmt);/*賦值寬度 */
                 else if (*fmt == '*') {/*???????*/
                         ++fmt;
                         /* it's the next argument */
                         field_width = va_arg(args, int);
                         if (field_width < 0) {
                                 field_width = -field_width;
                                 flags |= LEFT;
                         }
                 }

                 /* get the precision */
                 precision = -1;
                 if (*fmt == '.') {/*???%3.2d這之類的檢測?????*/
                         ++fmt;
                         if (isdigit(*fmt))
                                 precision = skip_atoi(&fmt);
                         else if (*fmt == '*') {
                                 ++fmt;
                                 /* it's the next argument */
                                 precision = va_arg(args, int);
                         }
                         if (precision < 0)
                                 precision = 0;
                 }
 
                 /* get the conversion qualifier */
                 qualifier = -1;
                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
                         qualifier = *fmt;
                         ++fmt;
                 }
 
                 /* default base */
                 base = 10;/*????*/
 
                 switch (*fmt) {/*檢測哪種格式輸出*/
                 case 'c':
                         if (!(flags & LEFT))
                                 while (--field_width > 0)/*默認的fiel。。爲-1 */
                                         *str++ = ' ';/*把要輸出的內容,全部轉換成字符放入str*/
                         *str++ = (unsigned char)va_arg(args, int);
                         while (--field_width > 0)
                                 *str++ = ' ';
                         continue;
 
                 case 's':
                         s = va_arg(args, char *);
                         len = strnlen(s, precision);
 
                         if (!(flags & LEFT))
                                 while (len < field_width--)
                                         *str++ = ' ';
                         for (i = 0; i < len; ++i)
                                 *str++ = *s++;
                         while (len < field_width--)
                                 *str++ = ' ';
                         continue;
 
                 case 'p':
                         if (field_width == -1) {
                                 field_width = 2 * sizeof(void *);
                                 flags |= ZEROPAD;
                         }
                         str = number(str,
                                      (unsigned long)va_arg(args, void *), 16,
                                  field_width, precision, flags);
                         continue;
 
                 case 'n':
                         if (qualifier == 'l') {
                                 long *ip = va_arg(args, long *);
                                 *ip = (str - buf);
                         } else {
                                 int *ip = va_arg(args, int *);
                                 *ip = (str - buf);
                         }
                         continue;
 
                 case '%':/*百分號的輸出*/
                         *str++ = '%';
                         continue;
 
                         /* integer number formats - set up the flags and "break" */
                 case 'o':
                         base = 8;
                         break;
 
                 case 'x':
                         flags |= SMALL;
                 case 'X':
                         base = 16;
                         break;
 
                 case 'd':/*d 要跳走??*/
                 case 'i':
                         flags |= SIGN;/*flags的作用?d後這裏爲2*/
                 case 'u':/*第一次爲%d 即是這裏*fmt爲'd'爲什麼會從這裏跳*/
                         break;/*flags |= SIGN已經執行過了再從這裏跳出,基本語法 還是???*/
 
                 default:
                         *str++ = '%';
                         if (*fmt)
                                 *str++ = *fmt;
                         else
                                 --fmt;
                         continue;
                 }
                 if (qualifier == 'l')/*???*/
                         num = va_arg(args, unsigned long);/*按照長整形取參數*/
                 else if (qualifier == 'h') {
                         num = (unsigned short)va_arg(args, int);/*按照...取數*/
                         if (flags & SIGN)
                                 num = (short)num;
                 } else if (flags & SIGN)/*??d的時候flags=2 SIGN=2按照int 取數 判斷是不是有符號數*/
                         num = va_arg(args, int);/* (*(int *) (((args) += (_bnd (int,AUPBND))) - (_bnd (int, _AUPBND)))) 將一個int *的指針指向那裏 並將值賦給num */
                 else/*無符號數*/
                         num = va_arg(args, unsigned int);
                 str = number(str, num, base, field_width, precision, flags);/*將數字轉化爲字符串 放入str*/
         }
         *str = '\0';
         return str - buf;
 }       


int myprintf(const char *fmt, ...)
{
        char printf_buf[1024];
        va_list args;
        int printed;

        va_start(args, fmt);/*取得參數的第一個變量的地址 將其賦給fmt*/
        printed = myvsprintf(printf_buf, fmt, args);/* 將要打印的值放入buf中 */
        va_end(args);/*將參數值取NULL*/

        puts(printf_buf);

        return printed;
}


int main()
{
int apple=2,pear=3; 
char *p = "this is point p 's content!";

myprintf("%d\n%d\n%s\n",apple,pear,p);

return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章