C — Arrays
Declaration and initialization
#include <stdio.h>
int main() {
int numbers[5]; // Uninitialized
int zeros[5] = {0}; // All zeros
int primes[] = {2, 3, 5, 7, 11}; // Size inferred: 5
int partial[5] = {10, 20}; // {10, 20, 0, 0, 0}
// C99 designated initializers
int scores[5] = {[0] = 95, [3] = 87}; // {95, 0, 0, 87, 0}
printf("First: %d, Last: %d\n", numbers[0], numbers[4]);
return 0;
}
Array size
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
// Total bytes
printf("Bytes: %zu\n", sizeof(arr)); // 20 (5 × 4)
// Element count
size_t len = sizeof(arr) / sizeof(arr[0]);
printf("Length: %zu\n", len); // 5
return 0;
}
Accessing elements
#include <stdio.h>
int main() {
int nums[] = {10, 20, 30, 40, 50};
// Read elements
printf("First: %d\n", nums[0]); // 10
printf("Third: %d\n", nums[2]); // 30
// Modify elements
nums[1] = 99;
printf("Modified: %d\n", nums[1]); // 99
// Iterate over array
for (int i = 0; i < 5; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
Bounds checking
C does not check array bounds — access beyond the array is undefined:
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
// These are undefined behavior:
// printf("%d\n", arr[10]); // Reading beyond bounds
// arr[10] = 99; // Writing beyond bounds
// Always track array sizes manually
return 0;
}
Passing arrays to functions
Arrays decay to pointers — you must pass the size:
#include <stdio.h>
void printArray(const int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void reverse(int arr[], int size) {
for (int i = 0; i < size / 2; i++) {
int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}
int main() {
int nums[] = {1, 2, 3, 4, 5};
int size = sizeof(nums) / sizeof(nums[0]);
printArray(nums, size); // 1 2 3 4 5
reverse(nums, size);
printArray(nums, size); // 5 4 3 2 1
return 0;
}
Multi-dimensional arrays
#include <stdio.h>
int main() {
// 2D array (matrix)
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Access elements
printf("Element [1][2]: %d\n", matrix[1][2]); // 7
// Iterate
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%3d", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Array algorithms
#include <stdio.h>
// Linear search
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) return i;
}
return -1;
}
// Bubble sort
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Find min and max
void findMinMax(int arr[], int size, int *min, int *max) {
*min = *max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < *min) *min = arr[i];
if (arr[i] > *max) *max = arr[i];
}
}
int main() {
int nums[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(nums) / sizeof(nums[0]);
bubbleSort(nums, size);
printf("Sorted: ");
for (int i = 0; i < size; i++) {
printf("%d ", nums[i]);
}
printf("\n"); // 11 12 22 25 34 64 90
int min, max;
findMinMax(nums, size, &min, &max);
printf("Min: %d, Max: %d\n", min, max);
int idx = linearSearch(nums, size, 25);
printf("Found 25 at index: %d\n", idx);
return 0;
}
Variable-length arrays (VLA)
C99 allows arrays with runtime-determined size:
#include <stdio.h>
int main() {
int n;
printf("Enter array size: ");
scanf("%d", &n);
int arr[n]; // VLA — size determined at runtime
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Mini Practice
Write C code that:
- Creates a 2D array and prints its diagonal elements
- Implements bubble sort and sorts an array
- Searches for an element in an array
- Finds the second largest element in an array
Up Next
In the next lesson, you'll learn about Strings — working with text in C.
Related Topics
Frequently Asked Questions about Arrays
What is Arrays in C?
Arrays is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Arrays?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Arrays.
Why is Arrays important in C?
Arrays is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.