Declare and Initialize an Integer Array in C
In C, an integer array is declared using the int
data type followed by the array name and size. It can be initialized at the time of declaration or later in the program.
The elements in the array are stored in contiguous memory locations and can be accessed using index values starting from 0.
Examples of Declaring and Initializing an Integer Array
1. Declaring and Initializing an Integer Array at the Time of Declaration
In this example, we declare an integer array and initialize it with values at the same time. We then print all elements of the array.
main.c
#include <stdio.h>
int main() {
// Declare and initialize an integer array
int numbers[] = {10, 20, 30, 40, 50};
// Print all elements of the array
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We declare an integer array
numbers[]
and initialize it with values{10, 20, 30, 40, 50}
. - The size of the array is automatically determined based on the number of elements.
- We use a
for
loop to iterate through the array and print each element. - The loop variable
i
starts at 0 and goes up to 4, accessing each element usingnumbers[i]
. printf()
is used inside the loop to print each value.
Output:
10 20 30 40 50
2. Declaring an Integer Array and Assigning Values Later
In this example, we declare an integer array first and then assign values to each index separately.
main.c
#include <stdio.h>
int main() {
// Declare an integer array of size 5
int numbers[5];
// Assign values to the array elements
numbers[0] = 5;
numbers[1] = 10;
numbers[2] = 15;
numbers[3] = 20;
numbers[4] = 25;
// Print all elements of the array
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We declare an integer array
numbers[5]
with a size of 5. - We assign values to each index manually using
numbers[index] = value;
. - We use a
for
loop to iterate through the array and print each value. - The loop runs from index
0
to4
, accessing elements usingnumbers[i]
. printf()
prints each element in the array.
Output:
5 10 15 20 25
3. Initializing an Array with Zero Values
In this example, we initialize an integer array with all zero values and print them.
main.c
#include <stdio.h>
int main() {
// Declare and initialize an integer array with all zeros
int numbers[5] = {0};
// Print all elements of the array
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We declare an integer array
numbers[5]
and initialize it with{0}
. - When an array is initialized with a single zero, all other elements are set to zero by default.
- We use a
for
loop to iterate through the array and print each element. - The loop runs from index
0
to4
, accessing each element usingnumbers[i]
. printf()
prints each value, which are all zero.
Output:
0 0 0 0 0
Conclusion
In this tutorial, we explored different ways to declare and initialize integer arrays in C:
- Declaring and initializing an integer array at the time of declaration.
- Declaring an array and assigning values later.
- Initializing an array with all zero values.
Arrays provide an efficient way to store and manipulate multiple values in a structured manner. Understanding initialization techniques is essential for effective programming in C.