printing out an array in c from user input

Here is an example of how you can print out an array in C from user input:

#include <stdio.h>

int main() {
    int size;
    printf("Enter the size of the array: ");
    scanf("%d", &size);

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

    printf("The array elements are: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Explanation:

  • First, we include the stdio.h library, which provides functions for input and output operations.
  • The main() function is the entry point of the program.
  • We declare a variable size to store the size of the array.
  • We use printf to ask the user to enter the size of the array.
  • We use scanf to read the user's input and store it in the size variable.
  • We declare an array arr of size size to store the elements entered by the user.
  • We use printf to ask the user to enter the elements of the array.
  • We use a for loop to iterate size times and use scanf to read each element entered by the user and store it in the corresponding index of the arr array.
  • We use printf to print "The array elements are: ".
  • We use another for loop to iterate size times and use printf to print each element of the arr array.
  • Finally, we return 0 to indicate successful execution of the program.