0

I recently started learning C and wanted to know if there was a way to declare some integers, with the value given by the user. For example, the user types in 3. I would like to create 3 integers, example a, b and c. If the user types in 5, I'd like to create a, b, c, d, e.

Is there any way to do so?

James M
  • 18,506
  • 3
  • 48
  • 56
iG0tB00ts
  • 11
  • 1
  • 1
  • 2

3 Answers3

1

You want to make an array because you cannot declare an undefined number of single variables. Since you are a beginner i will give you a complete answer you can compile it if you wish :

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

int main(){
    int* arr,number,i;
    printf("Give number value: ");
    scanf("%d",&number);
    arr = malloc(sizeof(*arr) * number); // after the comment, it safeguards the code
    for(i=0;i<number;i++){
        printf("%d ",i);
    }
    return 0;
}

arr is a pointer variable and you use it as an array that has the size of int * number of variables you want.

Edeph
  • 732
  • 2
  • 12
  • 29
  • 2
    `arr = (int*)malloc(sizeof(int)*number);` would be better as `arr = malloc(sizeof(*arr) * number);` – Ed S. Apr 13 '13 at 18:07
  • Well why would it be better since he wanted int values in the array? – Edeph Apr 13 '13 at 18:18
  • 1
    I'm not sure I understand your question. It's better because A) You don't cast the return value of `malloc` in C. It is completely unnecessary and can hide an error on older compilers which allow implicit int. No experienced C programmer casts the return value of `malloc`. B) you use `sizeof(*some_ptr)` instead of `sizeof(int)` because it safeguards your code a bit. If the type of `some_ptr` changes in the future you don't have to change the `sizeof` part as well. It has the same effect; the compiler knows the type of the expression `*some_ptr`. – Ed S. Apr 13 '13 at 18:24
  • Indeed you are right but i used `sizeof(int)` so he can understand better how it works since he is a beginner; but you are right. I asked because i thought he wouldn't change the type. – Edeph Apr 13 '13 at 18:27
  • That's fair, but I would add and explain the better option. No reason to instill bad habits in a beginner. – Ed S. Apr 13 '13 at 18:29
0

What you need is an array of integers. When you received “count” value, you need to dynamically allocate the array of “count” items of type “int”.

For further details please take a look at malloc () function: http://linux.die.net/man/3/malloc

Paul
  • 1,262
  • 8
  • 16
0

I will make this easy for you.

What you can create is an array. An array is essentially a range of elements stored inside one name. If your not sure how to make/use arrays for your example... here is an example.

int main (void){
int i; //counter
int totalIntegers
int arrayVariableName[100]; //array that can store any amount(100 for this case)
                            //of variables inside.
printf("Enter total amount of variables");
    scanf("%d", &totalIntegers); //collect what the user types, pretend you type 5

for(i=0;i<totalIntegers;i++){ //this will loop 5 times from same example.
   printf("enter a number: ");
     scanf("%d",&arrayVariableName[i]); //will store numbers in array 0(which 
                                        // is holding the integer inside a), 
                                        // array 1(holding b), array 2(holding c)       
                                        //array 3(holding d), array 4(holding e).
   }

}

With an array and for loop, you can set the total amount which will allow user to punch in numbers that many times. If for instance you type 7 inside of 5, you can hold 7 variables (a,b,c,d,e,f,g). If you plan to make more than 100 integers, change it inside the array declaration. There is a method to set the limit to a unique amount you desire, the answer above me shows you how, look at it for reference.

To learn more, simply youtube search "array tutorial in c".

Adeel Anwar
  • 729
  • 1
  • 5
  • 8
  • 1
    He'd be better of using dynamic allocation. If the user enters a value > 100 your example results in UB. – Ed S. Apr 13 '13 at 18:31