Reputation: 13
I want to create a program that will print the value of the three parameter by using define in c
#include <stdio.h>
#define Print(num) printf("%d",n##num)
int main()
{
int i;
int n1=1, n2=2, n3=3;
for(i=1;i<=3;i++)Print(i);
}
the problem than n##num equal to ni and to n1,n2,n3. is there a way to get the values of i to set in num instead of "i"?
Upvotes: 1
Views: 263
Reputation: 225007
The preprocessor runs before the code is even compiled. You can't do loops in the preprocessor like that.
What you really want is an array:
#include <stdio.h>
int main()
{
int i;
int n[] = { 1, 2, 3 };
for(i=0;i<3;i++) printf("%d\n", n[i]);
}
Upvotes: 4