Arrays help us store many values in one place. Common operations let us add, find, or change these values easily.
0
0
Common array operations
Introduction
You want to keep a list of student scores and find the highest score.
You need to add a new number to a list of temperatures.
You want to check if a certain number is in your list.
You want to change a value in your list after finding it.
You want to print all values stored in your list.
Syntax
C
/* Define an array and its size */ #define SIZE 100 int array[SIZE]; int size = 0; // current number of elements /* Add element at the end */ if (size < SIZE) { array[size] = new_value; size++; } /* Find element by value */ int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == value_to_find) { find_index = i; break; } } /* Update element at index */ if (index >= 0 && index < size) { array[index] = new_value; } /* Print all elements */ for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n");
Arrays have fixed size in C, so you must check you don't add beyond capacity.
Indexing starts at 0, so first element is array[0].
Examples
This example shows adding two elements, finding one, updating it, and printing all.
C
int array[5]; int size = 0; // Add elements array[size++] = 10; array[size++] = 20; // Find 20 int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 20) { find_index = i; break; } } // Update element at found index if (find_index != -1) { array[find_index] = 25; } // Print all for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n");
This example shows what happens if you try to add more elements than the array can hold.
C
int array[3]; int size = 0; // Try to add 4 elements (one more than capacity) for (int i = 1; i <= 4; i++) { if (size < 3) { array[size++] = i * 10; } else { printf("Cannot add %d, array full\n", i * 10); } }
This example shows searching in an empty array returns -1 (not found).
C
int array[0]; int size = 0; // Trying to find in empty array int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 10) { find_index = i; break; } } printf("Find index: %d\n", find_index);
Sample Program
This program adds three numbers to an array, prints them, finds the number 10, updates it to 20, and prints the array again.
C
#include <stdio.h> int main() { int array[5]; int size = 0; // Add elements array[size++] = 5; array[size++] = 10; array[size++] = 15; printf("Array before update: "); for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); // Find element 10 int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 10) { find_index = i; break; } } // Update element if found if (find_index != -1) { array[find_index] = 20; } printf("Array after update: "); for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); return 0; }
OutputSuccess
Important Notes
Adding elements is O(1) if you add at the end and have space.
Finding elements is O(n) because you may check each element.
Updating is O(1) once you know the index.
Common mistake: forgetting to check array size before adding causes bugs.
Use arrays when you know max size and want fast access by index.
Summary
Arrays store multiple values in order.
Common operations: add, find, update, print.
Always check array size to avoid errors.