__attribute__ format
来源:互联网 发布:淘宝上优衣库代购真吗 编辑:程序博客网 时间:2024/06/05 15:18
This __attribute__ allows assigning printf-like orscanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This isexceptionally 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>34</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.
0 0
- __attribute__ format
- __attribute__ format
- __attribute__ ((format (printf, 2, 3)));
- __attribute__((format(printf,x,y)))
- __attribute__((format(printf, a, b)))
- __attribute__ ((format (printf, 2, 3))); 疑惑
- 内核源码中的printk——__attribute__ format
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- __attribute__
- 什么情况下需要建立mysql索引
- Java时间工具类——DateTime 个人整理
- MySQL如何有效的存储IP地址及字符串IP和数值之间如何转换
- 修改CButton类按钮背景颜色和文字颜色
- 在main函数退出后执行其他函数 http://blog.chinaunix.net/uid-22332450-id-1775058.html
- __attribute__ format
- Android Studio中导入Launcher2遇到的问题以及解决方案
- OLTP 联机事务处理
- js 控制锚点定位
- 猪:原来大数据都是骗“猪”的
- SQLITE3中的索引使用方法-如何避免使用字符串做索引
- asm diskgroup中的Usable_file_MB和Req_mir_free_MB
- Spring 注解Aop实现
- MAT(Memory Analyzer Tool)工具入门介绍