具有多个替换的C预处理器定义指令

pzfprimi  于 2023-01-25  发布在  其他
关注(0)|答案(1)|浏览(140)
/*
 * Recommended alloc parameters for "small" contexts that are never expected
 * to contain much data (for example, a context to contain a query plan).
 */

    #define ALLOCSET_SMALL_MINSIZE   0
    #define ALLOCSET_SMALL_INITSIZE  (1 * 1024)
    #define ALLOCSET_SMALL_MAXSIZE   (8 * 1024)
    #define ALLOCSET_SMALL_SIZES \
        ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

我不明白最后的马可,我用

printf("%d", ALLOCSET_SMALL_SIZES);

然后警告:

warning: too many arguments for format [-Wformat-extra-args]

并且也返回0。
我发现类似的帖子:#define directive with multiple replacements?
更新:跳过源代码,明白了。
类函数

foo(size_t,size_t,size_t)

就可以用

foo(ALLOCSET_DEFAULT_SIZES)
lp0sw83n

lp0sw83n1#

给定以下代码片段:

#define ALLOCSET_SMALL_SIZES \
        ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

ALLOCSET_SMALL_SIZES将替换为:

ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

替换为:

0,
(1 * 1024),
(8 * 1024)

它们是独立的参数。然后在对printf ()的调用中:

printf("%d", ALLOCSET_SMALL_SIZES);

ALLOCSET_SMALL_SIZES被替换为0, (1 * 1024), (8 * 1024)
或者,

printf ("%d", 0, (1 * 1024), (8 * 1024));

所以才有警告。

    • 修复:**

将其改为:

printf ("%d %d %d", 0, (1 * 1024), (8 * 1024));

参见:Why use Macros in C?What are C macros useful for?

相关问题