#include <stdio.h>
#include <stdlib.h>
int main() {
int capacity = 5; // initial capacity
int size = 0; // current number of elements
int *arr = malloc(sizeof(int) * capacity);
int num;
while (scanf("%d", &num) == 1) { // read integers until end of input
if (size == capacity) { // if array is full, double its capacity
capacity *= 2;
int *tmp = realloc(arr, sizeof(int) * capacity);
if (tmp == NULL) {
printf("Error: failed to reallocate memory\n");
free(arr);
return 1;
}
arr = tmp;
}
arr[size] = num;
size++;
}
printf("The array is:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: