Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

define in c get the value of parameter

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"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading