1D Array in C (Declaration, Input & Programs)
In this lesson: Learn 1D arrays in C — declaration, initialization, input/output, sum, max and search programs with output in Hindi & English.
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 (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)
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
// 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.
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
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
#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; }
Array Operations
Finding Sum, Average, Max, Min
#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; }
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
#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; }
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?
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?
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?
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?
How do you find the length of an array in C?
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.