在sprintf中的字符串内的C宏

Rav*_*avi 3 c macros printf padding

我使用宏格式化字符串副本.实例如下.下面给出的代码尝试在字符串的剩余部分填充空字符.

#include <stdio.h>

#define LEN     10
#define str(x)  #x

void main() {
    char a[LEN];
    int b = 3445;

    sprintf(a, "%-"str(LEN)"d", b);     // I want "%-10d" here
    printf("|%s|", a);
}
Run Code Online (Sandbox Code Playgroud)

当我用gcc -Wall prog.c它编译它时会发出以下警告.

warning: format ‘%LE’ expects argument of type ‘long double’, but argument 3 has type ‘int’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)

这意味着宏未被正确替换.任何人都可以在这里帮助我这里有什么问题.

Jen*_*edt 6

您必须再次评估参数str以查看字符串中的10

#define LEN     10
#define str_(x)  #x
#define str(x)  str_(x)
Run Code Online (Sandbox Code Playgroud)

你这样做的方式,参数str是直接stringyfied,因此LEN到达格式.