1 #ifndef     __PRINT_H_
2 #define __PRINT_H_
3
4 void print(char* fmt, ...);
5 void printch(char ch);
6 void printdec(int dec);
7 void printflt(double flt);
8 void printbin(int bin);
9 void printhex(int hex);
10 void printstr(char* str);
11
12 #define console_print(ch) putchar(ch)
13
14 #endif /*#ifndef __PRINT_H_*/
 #include <stdio.h>
2 #include <stdarg.h>
3 #include "print.h"
4
5 int main(void)
6 {
7 print("print: %c\n", 'c');
8 print("print %d\n", 1234567);
9 print("print: %f\n", 1234567.1234567);
10 print("print: %s\n", "string test");
11 print("print: %b\n", 0x12345ff);
12 print("print: %x\n", 0xabcdef);
13 print("print: %%\n");
14 return 0;
15 }
16
17 void print(char* fmt, ...)
18 {
19 double vargflt = 0;
20 int vargint = 0;
21 char* vargpch = NULL;
22 char vargch = 0;
23 char* pfmt = NULL;
24 va_list vp;
25
26 va_start(vp, fmt);
27 pfmt = fmt;
28
29 while(*pfmt)
30 {
31 if(*pfmt == '%')
32 {
33 switch(*(++pfmt))
34 {
35
36 case 'c':
37 vargch = va_arg(vp, int);
38 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
39 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
40 printch(vargch);
41 break;
42 case 'd':
43 case 'i':
44 vargint = va_arg(vp, int);
45 printdec(vargint);
46 break;
47 case 'f':
48 vargflt = va_arg(vp, double);
49 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
50 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
51 printflt(vargflt);
52 break;
53 case 's':
54 vargpch = va_arg(vp, char*);
55 printstr(vargpch);
56 break;
57 case 'b':
58 case 'B':
59 vargint = va_arg(vp, int);
60 printbin(vargint);
61 break;
62 case 'x':
63 case 'X':
64 vargint = va_arg(vp, int);
65 printhex(vargint);
66 break;
67 case '%':
68 printch('%');
69 break;
70 default:
71 break;
72 }
73 pfmt++;
74 }
75 else
76 {
77 printch(*pfmt++);
78 }
79 }
80 va_end(vp);
81 }
82
83 void printch(char ch)
84 {
85 console_print(ch);
86 }
87
88 void printdec(int dec)
89 {
90 if(dec==0)
91 {
92 return;
93 }
94 printdec(dec/10);
95 printch( (char)(dec%10 + '0'));
96 }
97
98 void printflt(double flt)
99 {
100 int icnt = 0;
101 int tmpint = 0;
102
103 tmpint = (int)flt;
104 printdec(tmpint);
105 printch('.');
106 flt = flt - tmpint;
107 tmpint = (int)(flt * 1000000);
108 printdec(tmpint);
109 }
110
111 void printstr(char* str)
112 {
113 while(*str)
114 {
115 printch(*str++);
116 }
117 }
118
119 void printbin(int bin)
120 {
121 if(bin == 0)
122 {
123 printstr("0b");
124 return;
125 }
126 printbin(bin/2);
127 printch( (char)(bin%2 + '0'));
128 }
129
130 void printhex(int hex)
131 {
132 if(hex==0)
133 {
134 printstr("0x");
135 return;
136 }
137 printhex(hex/16);
138 if(hex < 10)
139 {
140 printch((char)(hex%16 + '0'));
141 }
142 else
143 {
144 printch((char)(hex%16 - 10 + 'a' ));
145 }
146 }

Linux内核中的printf实现的更多相关文章

  1. Linux内核中的printf实现【转】

    转自:http://www.cnblogs.com/chenglei/archive/2009/08/06/1540702.html 从main.c中的printf开始读这个函数. 首先看printf ...

  2. Linux内核中双向链表的经典实现

    概要 前面一章"介绍双向链表并给出了C/C++/Java三种实现",本章继续对双向链表进行探讨,介绍的内容是Linux内核中双向链表的经典实现和用法.其中,也会涉及到Linux内核 ...

  3. (十)Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  4. Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  5. linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)

    学过C语言的伙伴都知道,曾经比较两个数,输出最大或最小的一个,或者是比较三个数,输出最大或者最小的那个,又或是两个数交换,又或是绝对值等等,其实这些算法在linux内核中通通都有实现,以下的代码是我从 ...

  6. linux内核中的排序接口--sort函数

    linux内核中的sort函数,其实跟我们所说的qsort函数很像,我们来看看qsort: qsort 的函数原型是 void qsort(void*base,size_t num,size_t wi ...

  7. C语言在linux内核中do while(0)妙用之法

    为什么说do while(0) 妙?因为它的确就是妙,而且在linux内核中实现是相当的妙,我们来看看内核中的相关代码: #define db_error(fmt, ...) \ do { \ fpr ...

  8. Linux内核中的常用宏container_of其实很简单【转】

    转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...

  9. Linux内核中的信号机制--一个简单的例子【转】

    本文转载自:http://blog.csdn.net/ce123_zhouwei/article/details/8562958 Linux内核中的信号机制--一个简单的例子 Author:ce123 ...

随机推荐

  1. Hadoop、Strom集群实现日志自动清理

    Hadoop+Storm集群,运行一段时间ZooKeeper等关键进程自动停止运行,经检测是系统磁盘不足 系统磁盘100%: 查找大文件后发现,空间均被ZooKeeper产生的log占用 find / ...

  2. Python全栈之路----常用模块----hashlib加密模块

    加密算法介绍 HASH       Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...

  3. 软件测试_Fiddler抓包工具二

    多数资料摘至:https://www.cnblogs.com/nihaorz/p/5455148.html Fiddler 的命令使用 如何打开命令行:左下方的小黑条命令行,点击它就可以输入命令了(a ...

  4. linux 下 mysql 常用命令

    linux 下 mysql 常用命令   阅读目录 ====================== 1.开启和关闭 1.1.开启 1.2.关闭 1.3.重启 2.登录 2.1.密码明文 2.2.密码密文 ...

  5. springAop整合自定义注解做方法日志配置(源码在附件)

    package com.aop.log.anno; import java.lang.annotation.ElementType; import java.lang.annotation.Reten ...

  6. Sqlsession 的理解

    MyBatis的持久化解决方案是将用户从原始的JDBC访问中解放出来,用户只需要定义需要操作的SQL语句,无须关注底层的JDBC操作,就可以以面向对象的方式来进行持久化层操作.底层数据库连接的获取,数 ...

  7. promise在angular中的基本使用

    promise在angular中的基本使用 <!DOCTYPE html> <html ng-app="myApp"> <head> <m ...

  8. jumpserver管理入门

    比较规范一点的步骤 1.先在用户管理查看用户组里,添加用户组 2.在用户管理查看用户里,添加用户 3.在资产管理里查看资产,添加资产.在批量增加那里可以使用表格快速导入添加 4.在授权管理里的系统用户 ...

  9. Base64编码解码JS

    function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...

  10. Java高级特性 第10节 IDEA和Eclipse整合JUnit测试框架

    一.IDEA整合Junit测试框架 1.安装插件 打开File菜单的下拉菜单settings[设置] : 点击左侧Plugins[插件]菜单 在输入框中输入JUnitGenerator 2.0,点击I ...