MTK优美代码赏析8:可变参数的C函数
2010-10-27 22:22
525 查看
对于可变参数函数,自己一直以来都很关注,尤其当自己刚才C#上转到C时,一度曾研究C上的面向对象,将类似printf的函数归类为面向对象里的函数多态,当时只是一个主观感受。今日仔细看了下代码的实现,发现使用C提供的公共接口是可以实现的,但这些接口是如何实现的就不知了。。。
文件:stdarg.h
gcc中的定义为:
/* Define the standard macros for the user,
if this invocation was from the user program. */
/* Define the standard macros for the user, if this invocation was from the user program. */
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L
#define va_copy(d,s) __builtin_va_copy(d,s)
#endif
下面看dbg_print(char *fmt,…)函数实现,部分内容摘自:《用库函数stdarg.h实现函数参数的可变》http://blog.sina.com.cn/s/blog_4ce3d23e0100efm3.html
代码
typedef int *va_list[1];
#define va_start(ap, parmN) (void)(*(ap) = __va_start(parmN))
#define va_arg(ap, type) __va_arg(*(ap), type)
#define va_copy(dest, src) ((void)(*(dest) = *(src)))
#define va_end(ap) ((void)(*(ap) = 0))
#undef tolower
#undef isdigit
#define isdigit(c) (((c) >= ’0′) && ((c) <= ’9′))
#define tolower(c) (((c)>=’A’ && (c)<=’Z')?((c)+0×20):(c))
文件:stdarg.h
gcc中的定义为:
/* Define the standard macros for the user,
if this invocation was from the user program. */
/* Define the standard macros for the user, if this invocation was from the user program. */
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L
#define va_copy(d,s) __builtin_va_copy(d,s)
#endif
下面看dbg_print(char *fmt,…)函数实现,部分内容摘自:《用库函数stdarg.h实现函数参数的可变》http://blog.sina.com.cn/s/blog_4ce3d23e0100efm3.html
代码
typedef int *va_list[1];
#define va_start(ap, parmN) (void)(*(ap) = __va_start(parmN))
#define va_arg(ap, type) __va_arg(*(ap), type)
#define va_copy(dest, src) ((void)(*(dest) = *(src)))
#define va_end(ap) ((void)(*(ap) = 0))
#undef tolower
#undef isdigit
#define isdigit(c) (((c) >= ’0′) && ((c) <= ’9′))
#define tolower(c) (((c)>=’A’ && (c)<=’Z')?((c)+0×20):(c))
相关文章推荐
- MTK优美代码赏析3:nvram存储和读取部分的分析
- MTK优美代码赏析1:二分查找
- MTK优美代码赏析3
- MTK优美代码赏析6:电话本里的快速排序和插入排序算法
- MTK优美代码赏析5:LCD_BlackWrite与MTK中断控制器
- MTK优美代码赏析4:MTK_MMI协议栈
- MTK优美代码赏析2:MenuItemMask_flag
- MTK优美代码赏析2:MenuItemMask_flag
- MTK优美代码赏析4:MTK_MMI协议栈
- MTK优美代码赏析1:二分查找
- MTK优美代码赏析4:MTK_MMI协议栈
- MTK优美代码赏析7:文件搜索与匹配
- Uboot优美代码赏析2:第一阶段_硬件平台启动start.s分析
- Uboot优美代码赏析1:目录结构和malkefile分析
- C函数和宏中的可变参数
- Linux优美代码赏析0.0:Makefile
- Uboot优美代码赏析1:目录结构和malkefile分析
- Uboot优美代码赏析2:第一阶段_硬件平台启动start.s分析
- (补充)Uboot优美代码赏析1.1:分散加载表u-boot.lds
- Android优美代码赏析0.0:Makefile