OneBite.Dev - Coding blog in a bite size

append item in array in C

Code snippet on how to append item in array in C

int array[100], size, item; scanf("%d", &size); for (int i = 0; i < size; i++) { scanf("%d", &array[i]); } scanf("%d", &item); array[size] = item; size++;

This code allows for appending an item to the end of an array. First, the size of the array and the items that are already in the array are declared and accepted through input from the user. Then, an item that needs to be added is also accepted as input from the user. Afterward, the item is appended to the end of the array by placing it at the index which is one larger than the size of the array. Lastly, the size of the array is incremented by one to take into account the newly added item.

c