A One-Dimensional Array (1D Array) in C programming is a linear data structure that stores a fixed-size, sequential collection of elements of the same data type under a single variable name. Instead of declaring individual variables like marks1, marks2, marks3, a 1D array allows you to store and access hundreds of related values using an index.
Consider a real-world scenario where a BCA student needs to compute the average marks of 60 students in a class. Declaring 60 separate variables is tedious, prone to errors, and makes sorting or searching virtually impossible. With a 1-D array, all 60 values are stored in contiguous (adjacent) memory locations and can be traversed effortlessly using a single for loop.
data_type array_name[array_size];
data_type: Specifies the type of data each element will hold (e.g., int, float, char).
array_name: A valid C identifier following standard variable naming rules.
array_size: An integer constant inside square brackets [] denoting the maximum number of elements the array can store.
int marks[5]; /* Declares an integer array that can hold 5 values */
float salary[10]; /* Declares a float array that can hold 10 values */
char grade[26]; /* Declares a character array of 26 letters */
In C programming, a one-dimensional array can be initialized in two primary ways: compile-time initialization and run-time initialization.
1. Compile-Time Initialization
Values are assigned directly at the time of declaration:
/* 1. Complete initialization */
int age[5] = {18, 19, 20, 21, 22};
/* 2. Partial initialization: remaining elements automatically become 0 */
int numbers[5] = {10, 20}; /* numbers[2], numbers[3], numbers[4] will be 0 */
/* 3. Initialization without specifying size: compiler deduces size as 4 */
int roll_no[] = {101, 102, 103, 104};
/* 4. Zero initialization: sets all 5 elements to zero */
int counter[5] = {0};
2. Run-Time (User-Input) Initialization
Using a standard for loop and scanf() to populate values dynamically from user keyboard input:
int arr[5], i;
for(i = 0; i < 5; i++) {
printf("Enter element %d: ", i);
scanf("%d", &arr[i]);
}
In C, array indexing is zero-based (indices start from 0 and end at size - 1). All elements reside in strictly consecutive memory addresses.
Element Index |
Syntax Access |
Assigned Value |
Memory Address (4-Byte int) |
|---|---|---|---|
Index 0 | arr[0] | 15 | 2000 (Base Address) |
Index 1 | arr[1] | 25 | 2004 |
Index 2 | arr[2] | 35 | 2008 |
Index 3 | arr[3] | 45 | 2012 |
Index 4 | arr[4] | 55 | 2016 |
Memory Address Formula: Address of arr[i] = Base Address + (i * sizeof(data_type)). For instance, if Base Address = 2000 and sizeof(int) = 4 bytes, then Address of arr[3] = 2000 + (3 * 4) = 2012.
This complete program demonstrates how to accept N integers from the user and print them in order:
#include <stdio.h>
int main() {
int arr[100], n, i;
printf("Enter number of elements (max 100): ");
scanf("%d", &n);
/* Reading elements */
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
/* Displaying elements */
printf("\nElements stored in the array:\n");
for(i = 0; i < n; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
Finding the maximum and minimum values in an array is one of the most frequently asked practical exam questions for BCA and BSc Computer Science students:
#include <stdio.h>
int main() {
int a[50], n, i;
int max, min;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
/* Initialize max and min with first element */
max = a[0];
min = a[0];
for(i = 1; i < n; i++) {
if(a[i] > max) {
max = a[i];
}
if(a[i] < min) {
min = a[i];
}
}
printf("\nMaximum element = %d\n", max);
printf("Minimum element = %d\n", min);
return 0;
}
No Bound Checking: The C compiler does not check whether an array index is within valid bounds. If you declare int a[5]; and try to write into a[10] = 50;, the program will compile without error but will overwrite adjacent memory, leading to garbage results, unexpected crashes, or a Segmentation Fault.
Always ensure loop counters run strictly between 0 and size - 1.
Test your understanding with these exam-tested dry runs, MCQs, and hands-on coding challenges specially prepared for BCA semester exams.
Part 1: Predict the Output (Dry-Run Practice)
Question 1: What will be the output of the following C code snippet?
#include <stdio.h>
int main() {
int arr[5] = {10, 20};
printf("%d, %d, %d", arr[0], arr[2], arr[4]);
return 0;
}
Answer & Explanation: Output is 10, 0, 0. When an array is partially initialized in C, all remaining uninitialized elements are automatically set to zero by default.
Question 2: What happens in this code?
#include <stdio.h>
int main() {
int a[3] = {1, 2, 3};
printf("%d", a[3]);
return 0;
}
Answer & Explanation: Index 3 is out of bounds for an array of size 3 (valid indices are 0, 1, 2). The code produces an undefined value (Garbage Value) or runtime crash because C does not perform boundary checks.
Part 2: Multiple Choice Questions (BCA Exam Preparation)
Q1. Which index represents the 4th element of a one-dimensional array?
A) 4
B) 3
C) 5
D) 1
Correct Answer: B) 3 (Because C arrays use zero-based indexing: index 0 is 1st, index 1 is 2nd, index 2 is 3rd, index 3 is 4th).
Q2. What is stored in the array name itself in C?
A) Value of the first element
B) Base address of the array (address of element 0)
C) Total size in bytes
D) Total number of elements
Correct Answer: B) Base address of the array.
Q3. If an integer takes 4 bytes, how much memory is allocated for int arr[10];?
A) 10 bytes
B) 20 bytes
C) 40 bytes
D) 80 bytes
Correct Answer: C) 40 bytes (10 elements × 4 bytes = 40 bytes).
Part 3: Hands-on Lab Assignments for Beginners
Assignment 1: Write a C program to reverse an array without using a second auxiliary array.
Logic Hint: Use two pointers or two loop counters start = 0 and end = n - 1, swap arr[start] with arr[end], and move inward until start >= end.
Assignment 2: Write a program to count even and odd numbers in a given 1D array of 10 integers.
Logic Hint: Loop through the array and use the modulus operator: if(arr[i] % 2 == 0) even_count++; else odd_count++;.
Part 4: Frequently Asked University Viva Questions
1. What are the limitations of a one-dimensional array in C?
Answer: The size must be known at compile time (fixed size), insertion and deletion require shifting elements, and it can only store homogeneous data (elements of the same type).
2. What is the difference between arr and &arr[0]?
Answer: In most contexts, both represent the exact same memory address (the base address of the array).