
Arrays in C
Arrays in C are a powerful way to store multiple values of the same type using a single variable name. You can think of them like boxes lined up in memory — each box holds one item, and you access it by its index.
? Basics of Arrays
? Declaration
int numbers[5] = {10, 20, 30, 40, 50};
Or let the compiler count them:
int numbers[] = {10, 20, 30};
? Accessing Elements
Indexes start from 0:
printf("%d", numbers[0]); // prints 10numbers[1] = 25; // changes second element to 25
? Looping Through an Array
#include <stdio.h>int main() { int scores[] = {85, 90, 78, 92, 88}; int length = sizeof(scores) / sizeof(scores[0]); for (int i = 0; i < length; i++) { printf("Score %d: %d\n", i + 1, scores[i]); } return 0;}
? Types of Arrays
1. One-Dimensional Array
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
2. Multi-Dimensional Array (2D)
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6}};
Access like: matrix[0][2]
? 3
?? Array Boundaries
C does not check array bounds! Be careful not to go out of range:
int arr[3] = {1, 2, 3};arr[5] = 10; // ? undefined behavior!
? Bonus: String as Char Array
char name[] = "Alice";printf("%s", name); // prints Alice
It's actually a character array: ['A', 'l', 'i', 'c', 'e', '\0']
Want help with:
2D array operations?
Sorting arrays?
Passing arrays to functions?