Arrays help us store many values of the same type together in one place. This makes it easy to organize and use data efficiently.
When you want to keep a list of student scores in a class.
When you need to store daily temperatures for a month.
When you want to hold multiple names or words for quick access.
When you want to process a series of numbers or characters in order.
When you want to pass many values to a function easily.
Syntax
C
/* Define an array of integers with 5 elements */int numbers[5];
/* Define and initialize an array of characters */char letters[3] = {'a', 'b', 'c'};
Arrays have a fixed size that you set when you create them.
All elements in an array must be of the same type.
Examples
This creates space for 3 integers but does not set their values yet.
C
int scores[3];
// An empty array to store 3 scores
Here, the array starts with values 90, 85, and 78.
C
int scores[3] = {90, 85, 78};
// Array with 3 scores initialized
We use '\0' to mark the end of the string in C.
C
char name[6] = {'J', 'o', 'h', 'n', '\0'};
// Character array to store a string 'John'
Arrays usually have at least one element, but zero-length arrays can exist in some cases.
C
int empty[0];
// An array with zero elements (rarely used)
Sample Program
This program creates an array of 5 ages and prints each one with its position.
C
#include <stdio.h>
int main() {
// Create an array to store 5 agesint ages[5] = {21, 25, 30, 22, 28};
// Print all ages
printf("Ages stored in the array:\n");
for (int index = 0; index < 5; index++) {
printf("Age at index %d is %d\n", index, ages[index]);
}
return0;
}
OutputSuccess
Important Notes
Arrays store data in continuous memory locations, which makes accessing elements fast.
Arrays have fixed size; you cannot change their length after creation.
Common mistake: Accessing outside the array size causes errors or crashes.
Summary
Arrays group many values of the same type together.
They help organize data for easy access and processing.
Use arrays when you know how many items you need to store.
Practice
(1/5)
1. Why do we need arrays in C programming?
easy
A. To create new data types
B. To store multiple values of the same type together
C. To perform mathematical calculations automatically
D. To store only one value at a time
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type in one place, making data management easier.
Step 2: Compare options with array purpose
Only To store multiple values of the same type together correctly describes storing multiple values together; others describe unrelated features.
Final Answer:
To store multiple values of the same type together -> Option B
Quick Check:
Arrays group same-type values = D [OK]
Hint: Arrays hold many same-type values together [OK]