C ARRAYS
An array in C is a collection of multiple values of the same data type stored
together under a single variable name. Instead of creating separate variables for each
value, you can use an array to store and manage them efficiently using index numbers.
These index numbers start from 0, allowing easy access to each element. Arrays
are especially useful when you need to work with lists, such as marks of students,
temperatures, or items in a menu.
The general syntax for declaring an array is:
data_type array_name[array_size];
data_type: Specifies the type of elements the array will hold (e.g., int, float, char).
array_name: The identifier for the array.
array_size: The number of elements the array can hold (must be a positive integer).
Example
int numbers[5]; // Declares an integer array named 'numbers' with 5 elements
float prices[10]; // Declares a float array named 'prices' with 10 elements
char name[20]; // Declares a character array (string) with space for 20 characters
Key Points
The array size must be a constant value defined at compile time.
If the size is omitted during the declaration, it must be determined from the
initialization.
C Array Initialization
Array initialization is the process of assigning values to the elements of an array at the
time of declaration. You can initialize arrays either partially or fully using curly braces
{}.
The initialization syntax is:
data_type array_name[array_size] = {value1, value2, ..., valueN};
Examples
1. Full Initialization
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes all elements
Here, the numbers array has 5 elements, each assigned a specific value.
2. Partial Initialization
int numbers[5] = {1, 2}; // Initializes first two elements; remaining are set to 0
When fewer values are provided, the remaining elements are initialized to zero (for
numeric types).
3. Implicit Size Determination
int numbers[] = {1, 2, 3}; // Size is automatically set to 3
The compiler determines if the size is omitted based on the number of elements provided.
4. Character Array Initialization
char name[] = "Alice"; // Automatically includes the null terminator '\0'
ACCESS ARRAY ELEMENTS
Array elements can be accessed using their index values. The index starts at 0, so the first
element is at position 0, the second at position 1, and so on.
Example
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize an array
// Accessing elements
printf("First element: %d\n", arr[0]);
printf("Third element: %d\n", arr[2]);
return 0;
}
Output
First element: 10
Third element: 30
Input and Output Array Elements
Taking user input for an array.
#include <stdio.h>
int main() {
int arr[3];
// Input elements
printf("Enter 3 integers: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
// Output elements
printf("You entered: ");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
Enter 3 integers: 5 10 15
You entered: 5 10 15
Printing array elements (already initialized).
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
// Print elements
for (int i = 0; i < 3; i++) {
printf("Element at index %d: %d\n", i, arr[i]);
}
return 0;
}
Output
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Advantages of Arrays in C
1. Efficient Memory Usage: Arrays provide a way to store data sequentially, reducing
memory wastage.
2. Easy Data Access: Elements can be accessed directly using their indices.
3. Compact Code: Using loops, operations can be performed on multiple elements,
reducing repetitive code.
4. Static Storage: Data is allocated contiguously, enabling faster access and manipulation.
5. Ideal for Fixed-Size Data: Arrays are perfect when the number of elements is known in
advance.
Disadvantages of Arrays in C
1. Fixed Size: The size of an array must be defined at the time of declaration, limiting
flexibility.
2. Homogeneous Data: Arrays can only store elements of the same data type.
3. No Built-in Bounds Checking: Accessing indices outside the declared range can lead
to undefined behavior.
4. Insertion and Deletion: These operations are time-consuming as they require shifting
elements.
5. Memory Allocation: If improperly handled, large arrays can lead to excessive memory
usage.
Types of Array in C
Arrays are a fundamental data structure in C programming that stores multiple
elements of the same type in a contiguous memory block. Based on their dimensions,
arrays in C can be categorized into One-dimensional and Multidimensional arrays.
Below, we’ll explore these types in detail.
1. One Dimensional Array in C
A one-dimensional array is the simplest form of an array, storing elements in a
single linear sequence. It can be visualized as a row of elements.
Declaration
data_type array_name[size];
Example
int arr[5]; // Declares an array of size 5
Initialization
int arr[5] = {1, 2, 3, 4, 5};
Accessing Elements
printf("%d", arr[2]); // Accesses the third element, which is 3
Use Cases:
Storing a list of numbers, such as marks or salaries.
Simple linear data handling.
2-Dimensional Array in C
2D array organizes data in rows and columns, making it ideal for matrix
representation.
Declaration
data_type array_name[rows][columns];
Example
int arr[3][4]; // Declares a 2D array with 3 rows and 4 columns
Initialization
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Points to Remember While Initializing a 2D Array:
The total number of elements must not exceed rows * columns.
Omitted elements are automatically initialized to 0.
Use nested braces for better readability.
Examples of 2D Array in C
Printing a Matrix
#include <stdio.h>
int main() {
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
123
456
Summing Elements of a 2D Array
#include <stdio.h>
int main() {
int arr[2][2] = {
{1, 2},
{3, 4}
};
int sum = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum += arr[i][j];
}
}
printf("Sum: %d\n", sum);
return 0;
}
}
}
return 0;
}