I'm trying to take a list of numbers from user as input and store it in an array. Later I'll be displaying this array using a printf function.

I'm trying to take this input in 2 ways: as user prompt input separated by spaces and as command line arguments.

As user prompt (non-command line)

int main(void){
printf("Please enter total number of characters to be sorted: ");
int arraysize = GetInt();

int nums[arraysize];

printf("Enter %i numbers followed by spaces: ", arraysize);

for (int i = 0; i < arraysize; i++)
    nums[i] = GetInt();

    // not giving the desired result

printf("You have entered %s\n", nums[arraysize]);

Using Command Line Argument

int main(int argc, char* argv[]){
if (argc < 2 || argc >11)
    printf("You'll have to enter at least 1 and atmost 10 arguments\n");

    return 1;

int nums[argc - 1];

for (int i = 0; i < argc; i++)
    nums[i] = atoi(argv[i + 1]);   

printf("You have entered %i", nums[]);


In either cases I'm unable to take an array from user and display it back as it is with spaces.

I saw in some discussions that this can be done using pointers. However I'm still in week 3 and am looking for a way that doesn't involve pointers. I intend to use this number array as input to my sort function rather than using the random numbers, for simplicity.

Can you please point out what am I doing wrong or not doing at all?


1 Answer 1


First, and just a little advice, when you share your code, also paste the #include section... just so it's easier for other people to copy/paste it inside the IDE and try it right away :D.

Now, I want to answer the Command Line Argument version of the code first:

-realize that your loop is actually going out of bounds. Please check how many times you're executing the loop against the size of the nums array.

-printf() can't deal with arrays, so it won't compile if you try to pass it the nums array. Instead, implement a code that iterates inside the array and prints each value the way you desire.

The other option is a little harder... If you want the user to input each value separated by spaces, then GetInt() is not the way to go, since it only accepts one int. Since a space is a character, then you would need to deal with strings or char*.

You can make a code that works this way, but it's a little more complex, since you need to move from string to numbers, taking into account that you must ignore the spaces and only store the numbers into the array. Once the array is populated as expected, then you can do almost the same as in the Command Line Argument version.

Instead, I would recommend working with loops to make the user enter one value at a time... but well, this is almost exactly the same as how find.c works.. so yeah.

I do encourage you to try and work out each version, and I hope this helps you. :)

  • Thank you Alejandro. This answered my question.
    – ABor
    Apr 28, 2016 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .