0

What I mean by the question is this. For example, I add a scanf function that asks for the size of an array, which in this case is integer n. Now I want to form a loop that asks repeatedly for user input, but only until the size of the array.

printf("Enter the number of elements of the first array: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i<n; i++){
  scanf("%d ", arr[i);

the problem is that I do not want to separate the user inputs with a newline variable. So instead of

10
20
30
40

I want it to be

10 20 30 40

How do I do this ?

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
ruff
  • 71
  • 4
  • `scanf("%d ", arr[i);` -> `scanf("%d ", arr[i]);`. `%d` can skip whitespace. So, `scanf("%d", arr[i])` can read number from "10\n20\n30\n40\n" or "10 20 30 40". – GooT Mar 04 '22 at 02:45
  • Don't put white space at the end of a `scanf()` format string — see [What is the effect of white space in a `scanf()` format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string). The `scanf()` family of functions pay no heed to newlines (except to treat them as white space, or when listed in a scan set (`%[…]`)). You automatically get the behaviour you're seeking. – Jonathan Leffler Mar 04 '22 at 03:52

1 Answers1

1

Just expanding a little on I love python's comment

You're very close with the code you have already. Short answer: In the loop, remove the space after '%d' and add an addressof operator before arr[i].

But your code probably won't compile at the moment because you actually can't declare an array unless it's size is known at compile time (int arr[n]). You can, however, dynamically allocate it in memory using malloc() and free() (from stdlib.h) as follows:

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

int main()
{
    int n;
    
    printf("Enter the number of elements of the first array: ");
    scanf("%d", &n);
    
    // Dynamically allocate the array (allows for variable size)
    int* arr = (int*)malloc(n * sizeof(int));

    for (int i = 0; i<n; i++) {
        // Remove the space after '%d'
        // Add addressof operator before arr[i]
        scanf("%d", &arr[i]);
    }

    // Don't forget to free the allocated memory!
    free(arr);

    return 0;
}

The reason this works is because scanf returns as soon as it finds the requested input (an int, in this case). When it returns though, everything that it hasn't read gets left in the input buffer. When scanf executes again, it still has the rest of the user's input to read and it returns after reading the next int, and this keeps repeating until the loop condition is satisfied.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Note that C99 introduced the concept of [variable-length arrays](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.2) (VLA). You can indeed define function-scope arrays that are sized at runtime. Indeed, when applied to such arrays, the `sizeof()` operator becomes a runtime operator, not just a compile time operator. – Jonathan Leffler Mar 04 '22 at 03:55
  • Granted, the OP should validate the value of `n` before using it as the dimension of an array — it must be strictly positive (not zero, not negative) and must not be too big (total size less than 1 MiB on Windows, possibly significantly less; Unix normally has a limit of 8 MiB). The same comment applies to your code too — you should check `n` before using it, but the size limit for dynamic memory allocation is usually much larger — probably in the GiB range, if not bigger. – Jonathan Leffler Mar 04 '22 at 04:00