Home > front end >  Declaring multiple int with for loops in C
Declaring multiple int with for loops in C

Time:10-07

I am trying to declare multiple int with the help of for loops in c.

The code almost looks like

for (int x = 0; x < 45; x  ){
    int valx = 10*x;
}

The goal is to have a variable called val0 = 0, val1 = 10, val2 = 20...... I also need to be able to reference these made int later in the code and modify them, etc. Is there a coinvent to do this? If not is there another effective way to declare 45 variables fast? Also, I know the code is wrong I am trying to see if there is something similar. I think someone mentioned to use struct. How would that work in regard to this?

CodePudding user response:

You want to create an array outside of the loop and assign to that.

int val[45];
for (int x = 0; x < 45; x  ){
    val[x] = 10*x;
}

It wouldn't make sense to create the variables inside of the loop because those variables would disappear after the loop finishes.

  • Related