🟡 Intermediate · Lesson 20
1D Arrays
What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations. Instead of declaring 10 separate integer variables, you can use one array of size 10.
Without Array vs With Array
// Without array (bad): 5 separate variables int m1, m2, m3, m4, m5; // With array (good): one declaration int marks[5]; // Array of 5 integers
Key properties:
- All elements are of the same type
- Elements stored in contiguous memory
- Each element accessed using its index (starting from 0!)
- Size is fixed at declaration (in C89/C90)
💡 Memory Layout
For int marks[5], if the array starts at address 1000:
marks[0]=1000, marks[1]=1004, marks[2]=1008, marks[3]=1012, marks[4]=1016
(Each int = 4 bytes, so addresses differ by 4)
Declaration and Initialization
C Language
// Method 1: Declare only (garbage values) int arr[5]; // Method 2: Declare and initialize int marks[5] = {90, 85, 78, 92, 88}; // Method 3: Partial init (rest become 0) int nums[5] = {10, 20}; // {10, 20, 0, 0, 0} // Method 4: Size auto-detected from values int primes[] = {2, 3, 5, 7, 11}; // Size = 5 // Method 5: Initialize all to zero int zeros[10] = {0}; // All elements = 0 // Other types float temps[7] = {36.5f, 37.0f, 36.8f}; char grades[5] = {'A', 'B', 'A', 'C', 'B'};
Accessing Array Elements
Use the index inside square brackets. Index starts at 0, ends at size-1.
C Language
int marks[5] = {90, 85, 78, 92, 88}; printf("%d\\n", marks[0]); // 90 (first element) printf("%d\\n", marks[4]); // 88 (last element, index = size-1) marks[2] = 95; // Change element at index 2 printf("%d\\n", marks[2]); // Now prints 95
⚠️ Index Out of Bounds
Accessing arr[5] when size is 5 is undefined behavior — it may crash, produce wrong output, or corrupt memory. C does NOT check bounds automatically! Always stay within 0 to size-1.
Array Input and Output
C Language – Input N elements and print
#include <stdio.h> int main() { int n; printf("How many elements? "); scanf("%d", &n); int arr[n]; // Input printf("Enter %d numbers:\\n", n); for (int i = 0; i < n; i++) { printf("arr[%d] = ", i); scanf("%d", &arr[i]); } // Output printf("Array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\\n"); return 0; }
How many elements? 5
Enter 5 numbers:
arr[0] = 10
arr[1] = 25
arr[2] = 8
arr[3] = 40
arr[4] = 15
Array: 10 25 8 40 15
Array Operations
Finding Sum, Average, Max, Min
C Language – Complete Array Stats
#include <stdio.h> int main() { int marks[] = {85, 92, 78, 96, 70}; int n = sizeof(marks) / sizeof(marks[0]); // Auto calculate size int sum = 0, max = marks[0], min = marks[0]; for (int i = 0; i < n; i++) { sum += marks[i]; if (marks[i] > max) max = marks[i]; if (marks[i] < min) min = marks[i]; } printf("Sum = %d\\n", sum); printf("Average = %.2f\\n", (float)sum / n); printf("Maximum = %d\\n", max); printf("Minimum = %d\\n", min); return 0; }
Sum = 421
Average = 84.20
Maximum = 96
Minimum = 70
Linear Search
C Language – Linear Search
#include <stdio.h> int main() { int arr[] = {10, 25, 8, 40, 15}; int n = 5, key, found = 0; printf("Search for: "); scanf("%d", &key); for (int i = 0; i < n; i++) { if (arr[i] == key) { printf("Found at index %d\\n", i); found = 1; break; } } if (!found) printf("Not found\\n"); return 0; }
Bubble Sort
C Language – Bubble Sort
#include <stdio.h> int main() { int arr[] = {64, 34, 25, 12, 22}; int n = 5; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } printf("Sorted: "); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
Sorted: 12 22 25 34 64
Summary
- Array = collection of same-type elements in contiguous memory
- Index starts at 0, ends at size - 1
- Declaration:
type name[size]; - Use
sizeof(arr)/sizeof(arr[0])to get array length - Always stay within bounds — C doesn't check automatically!
- Use
forloops to traverse, search, sort arrays
Frequently Asked Questions
What is a one-dimensional array in C?
A one-dimensional array is a list of elements of the same type stored in consecutive memory, accessed by a single index. For example
int marks[5]; holds five integers, and you reach each one with marks[0] through marks[4].How do you declare and initialise an array in C?
You declare it with a type, name and size, such as
int a[5];, and can initialise it at the same time with values in braces, like int a[5] = {1, 2, 3, 4, 5};. If you give fewer values than the size, the rest are set to zero.Why do array indexes start at 0 in C?
An array index is really an offset from the start of the array, so the first element sits at offset 0. That is why
a[0] is the first item and the last item of an array of size n is a[n-1].What happens if you access an array out of bounds in C?
C does not check array bounds, so reading or writing past the end is undefined behaviour — it may return garbage, corrupt other data, or crash. You must make sure every index stays between 0 and the array size minus one.
How do you find the length of an array in C?
For an array whose size is known at compile time, you can compute it with
sizeof(a) / sizeof(a[0]). This does not work once the array has decayed to a pointer, such as inside a function, where you must pass the size separately.💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.