printf可變引數的實現
我是大自然的搬運工。。。。。
print.h
- #ifndef __PRINT_H_
- #define __PRINT_H_
- void print(char* fmt, ...);
- void printch(char ch);
- void printdec(int dec);
- void printflt(double flt);
- void printbin(int bin);
- void printhex(int hex);
- void printstr(char* str);
-
#define console_print(ch) putchar(ch)
- #endif /*#ifndef __PRINT_H_*/
上面print函式為全功能的列印函式,可以實現類似printf的功能,printch實現單個字元的列印、printdec實現十進位制格式數字的列印,printflt實現浮點數的列印,printbin實現二進位制格式數字的列印,printhex實現十六進位制格式數字的列印,printstr實現字串的列印,console_print函式是串列埠單字元列印函式的巨集定義,這裡暫時用PC終端單字元列印函式putchar代替。在實際嵌入式環境下,替換成串列埠單字元列印函式即可。
print.c
-
#include <stdio.h>
- #include <stdarg.h>
- #include "print.h"
- int main(void)
- {
- print("print: %c\n", 'c');
- print("print %d\n", 1234567);
- print("print: %f\n", 1234567.1234567);
- print("print: %s\n", "string test");
- print("print: %b\n", 0x12345ff);
- print("print: %x\n", 0xabcdef);
-
print(
- return 0;
- }
- void print(char* fmt, ...)
- {
- double vargflt = 0;
- int vargint = 0;
- char* vargpch = NULL;
- char vargch = 0;
- char* pfmt = NULL;
- va_list vp;
- va_start(vp, fmt);
- pfmt = fmt;
- while(*pfmt)
- {
- if(*pfmt == '%')
- {
- switch(*(++pfmt))
- {
- case'c':
- vargch = va_arg(vp, int);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- printch(vargch);
- break;
- case'd':
- case'i':
- vargint = va_arg(vp, int);
- printdec(vargint);
- break;
- case'f':
- vargflt = va_arg(vp, double);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- printflt(vargflt);
- break;
- case's':
- vargpch = va_arg(vp, char*);
- printstr(vargpch);
- break;
- case'b':
- case'B':
- vargint = va_arg(vp, int);
- printbin(vargint);
- break;
- case'x':
- case'X':
- vargint = va_arg(vp, int);
- printhex(vargint);
- break;
- case'%':
- printch('%');
- break;
- default:
- break;
- }
- pfmt++;
- }
- else
- {
- printch(*pfmt++);
- }
- }
- va_end(vp);
- }
- void printch(char ch)
- {
- console_print(ch);
- }
- void printdec(int dec)
- {
- if(dec==0)
- {
- return;
- }
- printdec(dec/10);
- printch( (char)(dec%10 + '0'));
- }
- void printflt(double flt)
- {
- int icnt = 0;
- int tmpint = 0;
- tmpint = (int)flt;
- printdec(tmpint);
- printch('.');
- flt = flt - tmpint;
- tmpint = (int)(flt * 1000000);
- printdec(tmpint);
- }
- void printstr(char* str)
- {
- while(*str)
- {
- printch(*str++);
- }
- }
- void printbin(int bin)
- {
- if(bin == 0)
- {
- printstr("0b");
- return;
- }
- printbin(bin/2);
- printch( (char)(bin%2 + '0'));
- }
- void printhex(int hex)
- {
- if(hex==0)
- {
- printstr("0x");
- return;
- }
- printhex(hex/16);
- if(hex < 10)
- {
- printch((char)(hex%16 + '0'));
-
相關推薦
printf可變引數的實現
我是大自然的搬運工。。。。。 print.h #ifndef __PRINT_H_ #define __PRINT_H_ void print(char* fmt, ...); void printch(char ch); vo
如何用 linux 實現命令列引數(可變引數實現)
僅用main函式的引數實現一個整數計算器 #include <stdio.h> #include <string.h> #include <stdlib.h>
理解printf可變引數函式
瞭解了下可變參函式的原理,這裡記錄下方便自己以後看printf函式原型 int printf(const char *fmt, ...)這裡寫個例子方便理解 printf("%s\n","hello"); 函式傳參引數壓棧的方向是從右往左,也就是說當呼叫printf函式的時候
通過可變引數實現函式,求函式的平均值
#include<stdio.h>#include<stdarg.h>#include<Windows.h>#pragma warning (disable :4996)int getaver(int num,...){va_list ar
05 printf函式可變引數的實現原理之彙編分析
如實現一個像printf函式格式的函式: test.c void myprintf(char *line, ...) // line指標變數是區域性變數,在棧裡分配空間 { printf(line); //呼叫printf時,r0存放字串地
用可變引數列表模擬實現printf函式
用可變引數列表實現printf函式要實現printf函式,我們首先應該獲取printf函式中由雙引號中的字串,若遇到不是%的字元,我們應該直接將其輸出,若遇到%,我們應該判斷它後面是什麼字元,這裡先只考慮%c,%s的情況,如果是%c那麼我們應該將讀到的字元輸出來,若為%s,應
【C語言】用可變引數列表實現printf函式
//用可變引數列表實現printf函式 #include <stdio.h> #include <stdarg.h> void my_printf(const char *st
模擬實現printf函式,可變引數列表例項
首先可通過CSDN檢視printf函式原型等資訊 實現功能: Print formatted output to the standard output stream. 即格式化輸出並
用可變引數列表實現簡單的printf函式
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<stdarg.h> void print_num(int n) { if (
用可變引數列表模擬實現printf函式,可完成下面的功能
【問題描述】:模擬實現printf函式,可完成下面的功能//能完成下面函式的呼叫。//print("s ccc d.\n","hello",'b','a','t',100);//函式原型://print
【c語言】用可變引數列表實現一個簡化的printf函式
//實現一個簡化的printf函式。 #include <stdarg.h> #include <string.h> void my_printf(const char *str,...) { va_list arg; //準備訪問可變引
可變引數列表(模擬實現printf)
1.什麼是可變引數列表?通過將函式實現為可變引數的形式,可以使得函式可以接受1個以上的任意多個引數。我們先看一個例子:int average(int n,...) { va_list arg; //相當於宣告一個指標,名字為arg int i =
可變引數模板--案例2(實現自己的printf)
利用可變模板引數自定義printf函式,直接上程式碼。 /******************************************* * Name: example2.cpp * Describe: 可變模板引數例項2:重寫printf * Au
實現自己的printf列印 -- 可變引數的函式
/*X86平臺,引數傳遞是基於堆疊來完成的,對記憶體使用時連續的*/ void printf_myself(const char *format, ...) { //char *ptr_s = &format; int num; char
使用可變引數,實現函式,求函式引數的平均值
使用可變引數,實現函式,求函式引數的平均值 程式程式碼如下: #include <stdio.h> #include <stdarg.h> int Average(int n, ...) { va_list arg;
C/C++中用va_start/va_arg/va_end實現可變引數函式的原理
C/C++中用va_start/va_arg/va_end實現可變引數函式的原理與例項詳解 在C/C++中,我們經常會用到可變引數的函式(比如printf/snprintf等),本篇筆記旨在講解編譯器藉助va_start/va_arg/va
可變引數模板實現C++反射
1. 概要 本文描述一個通過C++可變引數模板實現C++反射機制的方法。該方法非常實用,在Nebula高效能網路框架中大量應用,實現了非常強大的動態載入動態建立功能。Nebula框架在coding.net的倉庫地址。 C++11的新特性–可變模版引數(variadic templat
C語言 可變引數列表的實現
使用可變引數列表,可以讓函式在不同場合接收不同數量的引數傳入,printf函式的格式化輸出,就是一個典型的例子。 printf("<格式化字串>", <參量表>); &
c語言中可變引數的實現
(一) 寫一個簡單的可變引數的C函式 下面我們來探討,如何寫一個簡單的可變引數的C函式。寫可變引數的C函式要在程式中用到以下這些巨集: void va_start( va_list arg_ptr, prev_param ); type va_arg( va_list
C可變引數函式 實現
C函式要在程式中用到以下這些巨集: void va_start( va_list arg_ptr, prev_param ); type va_arg( va_list arg_ptr, type ); void va_end( va_list arg_ptr )