With reference to the SO thread C Macro Token Concatenation involving a variable – is it possible?,
Is it at all possible to generate variable names at compile-time in C and C++?
something like
int count = 8;
for(i=0; i<count; i++) {
int var_%i% = i*i; // <--- magic here
}
I know I can use arrays for this case, but this is just an example just to explain what I mean.
If you are expecting to use the value of
ito generate the namevar_%i%(e.g. generating variablesvar_1,var_2, …,var_count), then no, that’s not possible at all. For one thing, that’s not even a compile-time operation. The value ofiisn’t known until runtime. Yes, you can tell what it will be (and maybe a compiler could with static analysis in a very simple case), but in general values are exclusively run-time concepts.If you just mean creating a variable called
var_i, why don’t you just name it that?Maybe it would help if you explained what problem you’re trying to solve by doing this. I guarantee there’s a better way to go about it.