可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配。

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:

/* like printf() but to standard error only */
extern void eprintf(const char *format, ...)__attribute__((format(printf, 1, 2)));  /* 1=format 2=params *//* printf only if debugging is at the desired level */
extern void dprintf(int dlevel, const char *format, ...)__attribute__((format(printf, 2, 3)));  /* 2=format 3=params */

With the functions so declared, the compiler will examine the argument lists

$ cat test.c
1  extern void eprintf(const char *format, ...)
2               __attribute__((format(printf, 1, 2)));
3
4  void foo()
5  {
6      eprintf("s=%s\n", 5);             /* error on this line */
7
8      eprintf("n=%d,%d,%d\n", 1, 2);    /* error on this line */
9  }
$ cc -Wall -c test.c
test.c: In function `foo':
test.c:6: warning: format argument is not a pointer (arg 2)
test.c:8: warning: too few arguments for format

Note that the "standard" library functions - printf and the like - are already understood by the compiler by default.


format的语法格式为:

format (archetype, string-index, first-to-check)
  其中,“archetype”指定是哪种风格;“string-index”指定传入函数的第几个参数是格式化字符串;“first-to-check”指定从函数的第几个参数开始按上述规则进行检查。
具体的使用如下所示:
__attribute__((format(printf, a, b)))
__attribute__((format(scanf, a, b)))
--------------------- 
作者:huangjh2018 
来源:CSDN 
原文:https://blog.csdn.net/huangjh2017/article/details/76944564 
版权声明:本文为博主原创文章,转载请附上博文链接!

__attribute__((format(printf,m,n)))相关推荐

  1. [zz]GNU C __attribute__ 机制简介

    GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(Function Attribute).变量属性(Variable Attrib ...

  2. __attribute__((packed))

    1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...

  3. GNU C __attribute__ 机制简介

    原文链接:http://afreez.blog.51cto.com/59057/7351 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attrib ...

  4. __attribute__机制介绍

    1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__attribute__机制. __attribute__可以设置函数属性(Function Attribute).变量 ...

  5. printf函数源码linux,再来一版简易的printf函数实现

    以前发过两版简易的串口printf函数实现,最近搞了一段时间Linux的库文件,回过头又有不同的理解. 这一版函数基于MSP430F169,%d %x %o %b的实现不再由自己编写函数,而是调用MS ...

  6. __attribute__ 详解

    GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(Function    Attribute).变量属性(Variable Att ...

  7. __attribute__中的constructor和destructor

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  8. __attribute__ 详解 1

    From http://hi.baidu.com/cygnusnow/blog/item/8b82000f871fcf2f6159f3de.html GNU C的一大特色(却不被初学者所知)就是__a ...

  9. GCC的attribute——format

    1 解释 format是GCC提供的众多attribute之一,这个attribute的作用是告知编译器检查传给相应函数的参数中的格式字符串: /* 这是用在声明的情况,用在定义也可以 */ /* 一 ...

最新文章

  1. 深入掌握Java技术 EJB调用原理分析
  2. tp5数组为什么要中括号_VBA数组与字典解决方案第7讲:为什么要采用数组公式(一)...
  3. Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器
  4. 在openstack环境中安装rackspace private cloud --1 环境准备
  5. arm linux gcc 编译,Linux arm-linux-gcc交叉编译环境配置
  6. 论文阅读 - TransNet and TransNet V2
  7. bzoj1562 [NOI2009]变换序列
  8. Android开发 入门篇(一)
  9. 图解CRM(客户关系管理)全流程
  10. java web热区链接_HTML图片热区map area的用法
  11. 数据库系统教程(第二版何玉洁)课后数据库上机实验答案
  12. 北京有哪些牛逼的中小型公司?
  13. 随想:区块链提供数字科技霸权之外的第三选项
  14. 童文、李烨:6G的9大挑战
  15. gta5 android版数据包,gta5手机版2.21g数据包
  16. ## STM32F103的光照、烟雾MQ2、有毒气体MQ135的ADC配置(寄存器版与库函数版
  17. 关于:使用 nslookup 验证加入域所需的 SRV 记录
  18. PMP备考大全:经典题库(敏捷管理第16期)
  19. 如何开启谷歌浏览器的实验室功能
  20. 普通人做自媒体短视频,现在自媒体平台还有啥赛道可做的?

热门文章

  1. 如何快速准确的识别出一个文件的具体类型
  2. js 跳转到 百度指定地址定位点
  3. 人工智能实战_第八次作业_手写数学式子识别_廖盈嘉
  4. PureComponent: setState未触发render渲染
  5. 二十九、制作首页的显示列表
  6. java7新添加的一些特性--转
  7. C语言指向结构体的指针的例子
  8. Jquery 小技巧
  9. 判断release模式_AbstractQueuedSynchronizer共享模式与基于Condition的等待/通知
  10. 市面上有哪几种门_实木门、原木门、模压门,各有门道不怕坑!