里面的sprintf串c宏 [英] C macro inside string in sprintf
本文介绍了里面的sprintf串c宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用格式化字符串复制宏。实施例在下面给出。 code给娄试图垫空字符字符串的剩余部分。
I use macro for formatted string copy. Example is given below. Code given bellow tries to pad null characters in remaining portion of string.
#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);
}
当我编译的gcc -Wall prog.c中
它提供了以下警告。
when I compile it with gcc -Wall prog.c
it gives following warnings.
warning: format ‘%LE’ expects argument of type ‘long double’, but argument 3 has type ‘int’ [-Wformat]
这意味着,宏未正确被取代。任何人可以帮助我在这里有什么错在这里。
This means that macro is not substituted properly. Can anybody help me here what is wrong here.
推荐答案
您不得不评估参数 STR
再一次看到10里面的字符串
You'd have to evaluate the argument to str
once more to see the 10 inside the string
#define LEN 10
#define str_(x) #x
#define str(x) str_(x)
你的方式做到了,参数为 STR
直接stringyfied,因此 LEN
抵达格式
the way you did it, the argument to str
is stringyfied directly, thus the LEN
arrived in the format.
这篇关于里面的sprintf串c宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文