c - C中定义函数的__attribute__(格式)有什么用?

标签 c gcc

我在一个C项目中看到这行代码,没看懂。

#define FMT_CHK(fmt, args)  __attribute__ ((format (printf, fmt, args)))

GNU官网上没有解释清楚(https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes)

__attribute__ ((format ()) 的用途是什么,应该如何使用?

最佳答案

因此,fmtargs 参数只是告诉您哪个参数具有格式,哪个参数具有实参。

void myprintf(const char *fmt, ...);
//            ^^ fmt = arg#1
//                             ^^ args = arg#2...

所以在这种情况下,正确的属性是:

__attribute__((format(printf, 1, 2)))

如果你有一个更长的函数声明...

void myprintf(obj *x, const char *fmt, int level, ...)
//                    ^^ format: arg#2
//                                                ^^ args: arg#4...
    __attribute__((format(printf, 2, 4)));

关于c - C中定义函数的__attribute__(格式)有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279426/

相关文章:

c - 如何在 IBM i-series (AS/400) 上查找对应于 CHGJOB INQMSGRPY(*DFT) 的 ILE C 函数

c - gcc 无法在 Mac 上运行

C 读取文件到动态列表、动态字符串

c - Malloc 在内存中分配相同的地址?

c - socket connect() 返回 errno EINVAL

c - 为什么 GCC -O2 和 -O3 优化会破坏这个程序?

c++ - 使用 C++ 的基本终端输出 - 问题

在 Windows XP 中编译 C

c++ - 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?

c - 如何在 C99 中正确内联和使用内联函数,更正链接失败?