1

This is a question that popped into my head when I had to write a program that included multiple variables that each stored a single digit of a number. Let's say I declared several variables in main (or some other function):

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        int n_1 = 1;
        int n_2 = 2;
        int n_3 = 3;
        int n_4 = 4;
        int n_5 = 5;
        /*
        More variables
                .
                .
                .
        followed by multiple printf()s
        */

        exit(EXIT_SUCCESS);
}

Assume due to some unfortunate circumstances that I need all those separate variables and I cannot use an array or use a different, more efficient solution.

If I wanted to print the value stored in each variable, I could of course just print each single variable with a separate call to printf() or a single long call to printf(). But could I achieve the same goal more efficiently. I am especially interested if it is possible to do this in a loop. A solution which is common in scripting languages is using the value of the iterator variable in a for loop to complete the name of the variable given that the name has a fixed structure as in:

for (int i; i < n; ++i) {
        printf("%d\n", n_i);
}

but this will obviously not work in C (and I know why so no need to point this out). So what is the take on this by experienced people?

7
  • 4
    Add their pointers to an array of pointers, and iterate/print them dereferenced. Commented Jun 9, 2015 at 15:43
  • 3
    scripts are interpreted, not pre-compiled and so you can get away with using the suffix. This is not possible in C. Just remove the restrictions and use an array for the variables or similar. Commented Jun 9, 2015 at 15:46
  • No. It's not possible to "makeup" variable names at runtime. What are those circumstances that prevent using arrays? Commented Jun 9, 2015 at 15:46
  • @BlueMoon, they are made up. I know that using an array is good enough. My interest is merely technical. Commented Jun 9, 2015 at 15:48
  • 1
    @brauner: given your constraints, that's pretty much the only option available. Note that this method is only more "efficient" in terms of making the code easier to write and read. It will not buy you anything in terms of runtime or memory use. Commented Jun 9, 2015 at 15:57

2 Answers 2

1

Here's one way to do this with an array of pointers:

#include <stdio.h>

#define countof(A) (sizeof(A) / sizeof(A[0]))

int main(void) {
    int n_1 = 1;
    int n_2 = 2;
    int n_3 = 3;
    int n_4 = 4;
    int n_5 = 5;

    int* vars[] = { &n_1, &n_2, &n_3, &n_4, &n_5 };

    for (int v = 0; v < countof(vars); v++) {
        printf("%d\n", *vars[v]);
    }
    return 0;
}

You could hack up a macro that generates vars for all n_ variables in a given range, I guess, to prevent having to type out its initializer.

It's hard to imagine the circumstances that prevent you from just using a direct array though. Anything you can do with a local variable in C, you can also do with an array element, including taking its address: they're both lvalues and, barring extensions, can be used in exactly the same contexts.

Sign up to request clarification or add additional context in comments.

1 Comment

@brauner it's not worth rolling back, but new users should know that main is guaranteed by the standard to return 0 when written without an explicit return statement. They absolutely can rely on that, it's perfectly fine practice to do so.
0

The answer is "no" because the program text is not interpreted, as is in scripting languages. You could declare the variables as an array and index the array to print the variables.

int a[5], i;
....
for (i=0; i<5; i++) printf("%d",a[i]);
printf("\n");

1 Comment

printf("%d ",a[i]); looks better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.