11 #ifndef __PRINT_H_ 2 2 #define __PRINT_H_ 3 3 4 4 void print(char* fmt, ...); 5 5 void printch(char ch); 6 6 void printdec(int dec); 7 7 void printflt(double flt); 8 8 void printbin(int bin); 9 9 void printhex(int hex); 1010 void printstr(char* str); 1111 1212 #define console_print(ch) putchar(ch) 1313 1414 #endif /*#ifndef __PRINT_H_*/ 15 16#include <stdio.h> 17 2 #include <stdarg.h> 18 3 #include "print.h" 19 4 20 5 int main(void) 21 6 { 22 7 print("print: %c\n", 'c'); 23 8 print("print %d\n", 1234567); 24 9 print("print: %f\n", 1234567.1234567); 25 10 print("print: %s\n", "string test"); 26 11 print("print: %b\n", 0x12345ff); 27 12 print("print: %x\n", 0xabcdef); 28 13 print("print: %%\n"); 29 14 return 0; 30 15 } 31 16 32 17 void print(char* fmt, ...) 33 18 { 34 19 double vargflt = 0; 35 20 int vargint = 0; 36 21 char* vargpch = NULL; 37 22 char vargch = 0; 38 23 char* pfmt = NULL; 39 24 va_list vp; 40 25 41 26 va_start(vp, fmt); 42 27 pfmt = fmt; 43 28 44 29 while(*pfmt) 45 30 { 46 31 if(*pfmt == '%') 47 32 { 48 33 switch(*(++pfmt)) 49 34 { 50 35 51 36 case 'c': 52 37 vargch = va_arg(vp, int); 53 38 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI 54 39 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */ 55 40 printch(vargch); 56 41 break; 57 42 case 'd': 58 43 case 'i': 59 44 vargint = va_arg(vp, int); 60 45 printdec(vargint); 61 46 break; 62 47 case 'f': 63 48 vargflt = va_arg(vp, double); 64 49 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI 65 50 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */ 66 51 printflt(vargflt); 67 52 break; 68 53 case 's': 69 54 vargpch = va_arg(vp, char*); 70 55 printstr(vargpch); 71 56 break; 72 57 case 'b': 73 58 case 'B': 74 59 vargint = va_arg(vp, int); 75 60 printbin(vargint); 76 61 break; 77 62 case 'x': 78 63 case 'X': 79 64 vargint = va_arg(vp, int); 80 65 printhex(vargint); 81 66 break; 82 67 case '%': 83 68 printch('%'); 84 69 break; 85 70 default: 86 71 break; 87 72 } 88 73 pfmt++; 89 74 } 90 75 else 91 76 { 92 77 printch(*pfmt++); 93 78 } 94 79 } 95 80 va_end(vp); 96 81 } 97 82 98 83 void printch(char ch) 99 84 { 100 85 console_print(ch); 101 86 } 102 87 103 88 void printdec(int dec) 104 89 { 105 90 if(dec==0) 106 91 { 107 92 return; 108 93 } 109 94 printdec(dec/10); 110 95 printch( (char)(dec%10 + '0')); 111 96 } 112 97 113 98 void printflt(double flt) 114 99 { 115100 int icnt = 0; 116101 int tmpint = 0; 117102 118103 tmpint = (int)flt; 119104 printdec(tmpint); 120105 printch('.'); 121106 flt = flt - tmpint; 122107 tmpint = (int)(flt * 1000000); 123108 printdec(tmpint); 124109 } 125110 126111 void printstr(char* str) 127112 { 128113 while(*str) 129114 { 130115 printch(*str++); 131116 } 132117 } 133118 134119 void printbin(int bin) 135120 { 136121 if(bin == 0) 137122 { 138123 printstr("0b"); 139124 return; 140125 } 141126 printbin(bin/2); 142127 printch( (char)(bin%2 + '0')); 143128 } 144129 145130 void printhex(int hex) 146131 { 147132 if(hex==0) 148133 { 149134 printstr("0x"); 150135 return; 151136 } 152137 printhex(hex/16); 153138 if(hex < 10) 154139 { 155140 printch((char)(hex%16 + '0')); 156141 } 157142 else 158143 { 159144 printch((char)(hex%16 - 10 + 'a' )); 160145 } 161146 }
Linux内核中的printf实现
Stella981
2021-10-11
1115 0 0
点赞
收藏
评论区
加载中...