`
mmdev
  • 浏览: 12950880 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

__attribute__ format

阅读更多

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.

分享到:
评论

相关推荐

    Serial-VC++6

    hCom=CreateFile(name,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL); if(INVALID_HANDLE_VALUE==hCom) { // errLog.Format("create com file faild,port...

    thl_r16_tinav2.0_hm1375验证通过_增加打印设备ID_20170824_1447.7z

    ret = sysfs_create_group(&dev->pdev->dev.kobj, &vfe_attribute_group); #ifdef CONFIG_ES dev->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1; dev->early_suspend.suspend = vfe_early_suspend...

    hm1375_tinav2.1验证通过_增加设备ID的读取显示_20170825_1333没有外层目录.7z

    ret = sysfs_create_group(&dev->pdev->dev.kobj, &vfe_attribute_group); #ifdef CONFIG_ES dev->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1; dev->early_suspend.suspend = vfe_early_suspend...

    Delphi 获取文件访问时间.rar

    Delphi 获取文件访问时间,也主是最后一次打开文件 的时间,... Result:=Format('%d年%d月%d日 %2.2d:%2.2d:%2.2d',[STime.wYear,STime.wMonth,STime.wDay,STime.wHour,STime.wMinute,STime.wSecond]);  end;  end;

    italian_job:ActiveModel验证程序,用于处理典型的意大利业务问题

    validates :my_attribute , presence : true , codice_fiscale_format : true end 可用的验证器 codice_fiscale_format :意大利税码称为“ Codice Fiscale”。 有关更多信息,请参见 。 partita_iva_format :...

    BDB Professional Edition v2.7

    Opened database physical structure file format(XML standard format). Supply the source program of the outer invocation database, including these three edition: C#/Delphi/JAVA. 11.Support multi-...

    基于VB客票中心售票系统

    NowTime = Format(Now, "yyyy-mm-dd_hh-mm-ss") backFile = "hotel" & NowTime sql = "BACKUP DATABASE hotel TO disk='" & App.Path & "\BackUP\" & backFile & "'" cn.Execute (sql) MsgBox "数据库备份成功!" & ...

    Android代码-SwitchButton

    format example strokeRadius dimension app:strokeRadius="5dp" strokeWidth dimension app:strokeWidth="2dp" textSize dimension app:textSize="16sp" selectedColor color/reference app:...

    show_for:用帮手包裹您的物品以轻松显示它们

    = u.attribute :created_at, format: :short %> <%= u.attribute :last_sign_in_at, if_blank: "User did not access yet", wrapper_html: { id: "sign_in_timestamp" } %> <%= u.attribute :photo...

    Python __setattr__、 __getattr__、 __delattr__、__call__用法示例

    getattr `getattr`函数属于内建函数,可以通过函数名称获取 复制代码 代码如下: value = obj.attribute value = getattr(obj, “attribute”) ...def output(data, format=”text”):   output_function = getatt

    JDK_1_6 API

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    hexfilepatcher:固件修补,更改Intel HEX文件中的变量值

    如何设置存储位置要使用gcc将变量/结构放在某个内存位置,请执行以下操作: volatile mystruct_t __attribute((section (".fwConfigSection"))) mystruct = { 0 };并使用链接器注册该部分: LDFLAGS += -Wl,--...

    VBA API 32 Serialport MT-SICS

    Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const OPEN_EXISTING = 3 ' COMM Functions Private Const MS_CTS_ON = &H10& Private Const MS_DSR_ON = &H20& Private Const MS_RING_ON = &H40& ...

    ch2.rar_VHDL/FPGA/Verilog_PDF_

    vhdl data typeSignal Attributes&#8226 Specific values associated with signals.&#8226 Format:signal_name attribute_designator

    Senfore_DragDrop_v4.1

    6) If your version of Delphi does not support text format DFM files (e.g. Delphi 4 doesn't), you will have to use the convert.exe utility supplied with Delphi to convert all the demo form files to ...

    python图标制作工具【jpg、png转ico】源码+成品.zip

    最近在cv做个博客,碰到了需求要弄个网站图标。... print('生成完毕,{} --> {}'.format(img_name, ico_name)) except (AttributeError, IOError): print("图片路径或尺寸选择错误") input("按任意键退出")

    GIT2.33.0.2最新版本.zip

    Git 2.33 Release Notes ====================== Updates since Git 2.32 ---------------------- UI, Workflows & Features * "git send-email" learned ... with __attribute__() to catch placeholder/parameter

    商品销售源码

    VERSION 5.00 Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "TABCTL32.OCX" Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX" Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90...

    weka使用指导

    data storage method is ARFF format (Section 2.4). You can easily conve. a spreadsheet to ARFF. The bulk of an ARFF file consists of a list of the ins and the attribute values for each instance are ...

    MVPCStruct:在Swift中打包C结构的类

    Swift的C结构处理 在Swift中打包和解压缩C结构的类,以Python中的模块为模型。 样例代码 import MVPCStruct // Receiver expects a message with a header like this: // typedef struct { ...// } __attribute__

Global site tag (gtag.js) - Google Analytics