__attribute__((format(printf, a, b)))
最近,在看libevent源码,第一次看到__attribute__((format(printf, a, b)))这种写法。因此,在这里记录下用法。
功能:__attribute__ format属性可以给被声明的函数加上类似printf或者scanf的特征,它可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配。format属性告诉编译器,按照printf, scanf等标准C函数参数格式规则对该函数的参数进行检查。这在我们自己封装调试信息的接口时非常的有用。
format的语法格式为:
format (archetype, string-index, first-to-check)
其中,“archetype”指定是哪种风格;“string-index”指定传入函数的第几个参数是格式化字符串;“first-to-check”指定从函数的第几个参数开始按上述规则进行检查。
具体的使用如下所示:
__attribute__((format(printf, a, b)))
__attribute__((format(scanf, a, b)))
其中参数m与n的含义为:
a:第几个参数为格式化字符串(format string);
b:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几。
下面直接给个例子来说明:
#include <stdio.h>
#include <stdarg.h>
#if 1
#define CHECK_FMT(a, b) __attribute__((format(printf, a, b)))
#else
#define CHECK_FMT(a, b)
#endif
void TRACE(const char *fmt, ...) CHECK_FMT(1, 2);
void TRACE(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)printf(fmt, ap);
va_end(ap);
}
int main(void)
{
TRACE("iValue = %d\n", 6);
TRACE("iValue = %d\n", "test");
return 0;
}
注意:需要打开警告信息即(-Wall)。
编译结果如下所示:
main.cpp: In function ‘int main()’:
main.cpp:26:31: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘const char*’ [-Wformat=]
TRACE("iValue = %d\n", "test");
如果不使用__attribute__ format则不会有警告。
__attribute__((format(printf, a, b)))的更多相关文章
- printf 参数检查 __attribute__((format(printf, 1, 2)))
With GCC, I can specify __attribute__((format(printf, 1, 2))) , telling the compiler that this funct ...
- OC中的__attribute__的使用
简介: 在IOS9.2官方文档中Attributes的描述如下,简单明了: Attributes provide more information about a declaration or typ ...
- iOS宏和__attribute__
本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...
- __attribute__
转来的: http://www.cnblogs.com/astwish/p/3460618.html __attribute__ 你知多少? GNU C 的一大特色就是__attribute__ 机制 ...
- [c++]printf的编译器静态检测
经常会用到C的格式化字符串,gcc/clang的扩展,都是可以进行字符串和可变参数的检测,不匹配,就会爆warning. 自己手写的,需要加上一点代码,否则不会进行检测. 1. 可变参数 void ...
- __attribute__ 你知多少?
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和 ...
- __ATTRIBUTE__ 你知多少?【转】
转自:http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属 ...
- __attribute__你知多少(转)
转自:http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属 ...
- __attribute__机制介绍
转载:http://blog.csdn.net/ithomer/article/details/6566739 1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__att ...
随机推荐
- EM算法-完整推导
前篇已经对EM过程,举了扔硬币和高斯分布等案例来直观认识了, 目标是参数估计, 分为 E-step 和 M-step, 不断循环, 直到收敛则求出了近似的估计参数, 不多说了, 本篇不说栗子, 直接来 ...
- osi7层
- React 借助pubsub-js进行兄弟组件的传递值
1===> raect中两个 兄弟组件 互相通信使用的技术 使用 消息订阅(subscribe)和发布(publish)机制 s儿 伯 s rai b pʌ b lɪ ʃ 有一个库可以处理 Pu ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第十六周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第十六周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...
- LeetCode 53. Maximum Subarray最大子序和 (C++)
题目: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LG5338/BZOJ5509/LOJ3105 「TJOI2019」甲苯先生的滚榜 Treap
问题描述 LG5338 LOJ3105 BZOJ5509 题解 建立一棵\(\mathrm{Treap}\),把原来的\(val\)换成两个值\(ac,tim\) 原来的比较\(val_a<va ...
- 组件注册-自定义TypeFilter指定过滤规则
组件注册-自定义TypeFilter指定过滤规则 4.1 FilterType.ANNOTATION 按照注解方式 4.2 FilterType.ASSIGNABLE_TYPE 按照给定的类型 @Co ...
- Apex 的 API 简介
Salesforce 数据API分类 Salesforce中提供了四种数据API接口.数据API接口为开发者提供了操作Salesforce数据的能力.它们分别是: REST API:依据RESTful ...
- Shell编程——位置参数变量
1. (1)创建Shell脚本: (2)赋予执行权限: (3)执行Shell脚本: (4)对脚本赋值后执行脚本: $n:n为数字,$0代表命令本身,$1-9代表第一到第九个参数,十以上的参数用大括号包 ...
- [RN] windows7 安装 Realm Studio 后,打开报错 A JavaScript error occurred in the main process
windows7 安装 Realm Studio 后,打开报错 报错如下: A JavaScript error occurred in the main process Uncaught Exce ...