__attribute__ format
2015-09-22 13:42
267 查看
This __attribute__ allows assigning printf-like or
scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is
exceptionally helpful in tracking down hard-to-find bugs.
There are two flavors:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
but in practice we use the first one much more often.
The (m) is the number of the "format string" parameter, and (n) is the number of the first variadic parameter. To see some examples:
With the functions so declared, the compiler will examine the argument lists
Note that the "standard" library functions - printf and the like - are already understood by the compiler by default.
scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is
exceptionally helpful in tracking down hard-to-find bugs.
There are two flavors:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
but in practice we use the first one much more often.
The (m) is the number of the "format string" parameter, and (n) is the number of the first variadic parameter. To see some examples:
/* like printf() but to standard error only */ <strong>extern void eprintf(const char *format, ...) __attribute__((format(printf, 1, 2)))</strong>; /* <em>1=format 2=params</em> */ /* printf only if debugging is at the desired level */ <strong>extern void dprintf(int dlevel, const char *format, ...) __attribute__((format(printf, 2, 3)))</strong>; /* <em>2=format 3=params</em> */
With the functions so declared, the compiler will examine the argument lists
$ <strong>cat test.c</strong> <em>1</em> extern void eprintf(const char *format, ...) <em>2</em> <strong>__attribute__((format(printf, 1, 2)))</strong>; <em>3 4</em> void foo() <em>5</em> { <strong><em>6</em> eprintf("s=%s/n", 5);</strong> /* <em>error on this line</em> */ <em>7</em> <strong><em>8</em> eprintf("n=%d,%d,%d/n", 1, 2);</strong> /* <em>error on this line</em> */ <em>9</em> } $ <strong>cc -Wall -c test.c</strong> test.c: In function `foo': <strong>test.c:6</strong>: warning: format argument is not a pointer (arg 2) <strong>test.c:8</strong>: warning: too few arguments for format
Note that the "standard" library functions - printf and the like - are already understood by the compiler by default.
相关文章推荐
- Android Studio-Tip of the Day-每日贴士
- junit测试出现的小问题解决方案
- 格灵深瞳:计算机视觉是改变了什么
- Android Studio中导入Launcher2遇到的问题以及解决方案
- Objective-C类型推断
- Java时间工具类——DateTime 个人整理
- __attribute__ noreturn
- 修改CButton类按钮背景颜色和文字颜色
- jQuery.extend 函数详解
- sublime3的配置
- 什么情况下需要建立mysql索引
- ElasticSearch线程池配置
- 运用cocoapods管理三方开源框架
- HttpSessionListener中使用依赖注入的方式完成Bean实例的注入
- [BLE--SMP]蓝牙的SMP安全管理简介
- java学习之IO字符流
- Debug VoltDB EE部分代码
- crf++-0.58java调用
- DP:Coins(POJ 1742)
- MySQL日期时间函数大全 转