变参数print函数的实现va_list
来源:互联网 发布:小区网络监控设计方案 编辑:程序博客网 时间:2024/06/05 03:01
implement print function without print .use putchar
http://blog.csdn.net/self_chou/article/details/12858805void ;
va_start(vp, fmt); point to current parameter ,eg: print %c\n
va_arg(vp,int) point to next parameter ,eg: c
va_end set null
----------------------------------------------------
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)
#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("print: %%\n");
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':
//felixzh1
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'));
}
else
{
printch((char)(hex%16 - 10 + 'a' ));
}
}
0 0
- 变参数print函数的实现va_list
- 有关VA_LIST的用法--变参函数的实现
- 有关VA_LIST的用法--变参函数的实现
- 用va_list实现变参函数
- va_list可变参数的函数
- va_list实现不定参数C函数
- va_list实现不定参数C函数
- va_list实现不定参数C函数
- 变参函数和va_list
- 奇特的可变参数列表实现print函数
- OC可变参数的函数实现va_start、va_end、va_list的使用
- va_start,va_arg,va_end,va_list应用举例--实现可变参数的函数
- 可变参数函数printf的实现(不使用va_list等内置宏定义)
- va_list、va_start、va_end(用于实现函数参数的个数可变的函数,如scanf()/printf()系列函数)
- 使用va_list自定义print的小例子.
- print函数的各个参数含义
- R语言 print函数的参数
- 关于C中函数的可变参数va_list...
- 信息系统项目管理师考试经验谈
- 1. debian telnet安装
- Java线程生命周期与基本操作(二)
- 在多继承的时候,如果一个类继承同时继承自class A和class B,而class A和B中都有一个函数叫foo(),如何明确的在子类中指出override哪个父类的foo()?
- [LeetCode] Validate Binary Search Tree
- 变参数print函数的实现va_list
- Android的ContentProvider(一)
- nginx按天分割日志
- android:屏幕自适应
- iOS UIPageControl简单实例
- ORACLE配置tnsnames.ora文件实例
- Unity爆炸检测
- linux中用wine加载使用TortoiseSVN
- 【ural】1960. Palindromes and Super Abilities【Palindromic Tree】