I'm running into the following problem when trying to use concatenation with the C preprocessor:
#define substitute(id) var##id
int main()
{
int var0 = 999;
int var1 = 998;
int var2 = 997;
int var3 = 996;
int var4 = 995;
int i = 0;
for(i; i < 5; i++)
{
printf("Valor: %i \n", substitute(i));
}
system("PAUSE");
return 0;
}
Is there a way for the preprocessor to be able to read the value on "i" instead of just concatenating "vari"?
No. The preprocessor works before compilation and therefore before execution.
The define
will cause your loop to expand to:
The preprocessor has no knowledge of the variable i, nor should it.
You should probably use an array:
and access it via
[]
: