Declare and Initialize Integer Array
In this step, you will learn how to declare and initialize an integer array in C, which will serve as the source for finding even numbers.
- In the WebIDE, add the following code to your
even_numbers.c
file:
#include <stdio.h>
int main() {
// Declare and initialize an integer array
int numbers[] = {21, 78, 62, 90, 55, 10, 85, 45, 11, 2};
// Calculate the size of the array
int size = sizeof(numbers) / sizeof(numbers[0]);
}
-
Let's break down the array declaration:
int numbers[]
declares an integer array
{21, 78, 62, 90, 55, 10, 85, 45, 11, 2}
initializes the array with specific integer values
sizeof(numbers) / sizeof(numbers[0])
calculates the total number of elements in the array
-
Add a print statement to verify the array size:
#include <stdio.h>
int main() {
int numbers[] = {21, 78, 62, 90, 55, 10, 85, 45, 11, 2};
int size = sizeof(numbers) / sizeof(numbers[0]);
// Print the array size
printf("Array size: %d\n", size);
return 0;
}
Compile and run the program to check the array size.
gcc even_numbers.c -o even_numbers
./even_numbers
Example output:
Array size: 10