Reputation: 1710
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);
}
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.
Upvotes: 4
Views: 2323
Reputation: 78923
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)
the way you did it, the argument to str
is stringyfied directly, thus the LEN
arrived in the format.
Upvotes: 6