Linux内核中的printf实现

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 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Linux内核中的printf实现 - HelloWorld