加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

这个C代码是什么意思(G_GNUC_PRINTF)?

发布时间:2020-12-16 10:12:53 所属栏目:百科 来源:网络整理
导读:static void ddict_debug(const char* fmt,...) G_GNUC_PRINTF(1,2); 我在.c文件中发现了这个,我不明白这一行: 是否只有一个函数声明或两个? 这段代码是什么意思? 解决方法 G_GNUC_PRINTF()是一个glib库预处理器宏. 对于gcc编译器,它定义如下(来自glib-2.
static void ddict_debug(const char* fmt,...) G_GNUC_PRINTF(1,2);

我在.c文件中发现了这个,我不明白这一行:
是否只有一个函数声明或两个?

这段代码是什么意思?

解决方法

G_GNUC_PRINTF()是一个glib库预处理器宏.
对于gcc编译器,它定义如下(来自glib-2.4.5 / glib / gmacros.h):

#define G_GNUC_PRINTF( format_idx,arg_idx )    
  __attribute__((__format__ (__printf__,format_idx,arg_idx)))

从gnome documentation:

Expands to the GNU C format function attribute if the compiler is gcc. This is used for declaring functions which take a variable number of arguments,with the same syntax as printf(). It allows the compiler to type-check the arguments passed to the function.

Place the attribute after the function declaration,just before the semicolon.

Parameters:

format_idx: the index of the argument corresponding to the format string (The arguments are numbered from 1)

arg_idx: the index of the first of the format arguments

例1:

static void ddict_debug(const char* fmt,2);
//                                   |    |                 |  |
// format string,format_idx = 1 ----+    |            <----+  | 
// format arguments,arg_idx = 2 ---------+            <-------+

例2:

static void foo_debug(int foo,const char* fmt,...) G_GNUC_PRINTF(2,3);
//                         |                |    |                 |  |
// not a printf argument --+                |    |                 |  |
// format string,format_idx = 2 -----------+    |            <----+  |
// format arguments,arg_idx = 3 ----------------+            <-------+

摘要:

Is there just one function declaration or two ?

一个类似printf()的函数被定义.宏告诉编译器对传递给函数的参数进行类型检查.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读