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 thesize
variable. - We declare an array
arr
of sizesize
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 iteratesize
times and usescanf
to read each element entered by the user and store it in the corresponding index of thearr
array. - We use
printf
to print "The array elements are: ". - We use another
for
loop to iteratesize
times and useprintf
to print each element of thearr
array. - Finally, we return 0 to indicate successful execution of the program.