Declarar e Inicializar Array de Inteiros
Nesta etapa, você aprenderá como declarar e inicializar um array de inteiros em C, que servirá como fonte para encontrar números pares.
- No WebIDE, adicione o seguinte código ao seu arquivo
even_numbers.c:
#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]);
}
-
Vamos detalhar a declaração do array:
int numbers[] declara um array de inteiros
{21, 78, 62, 90, 55, 10, 85, 45, 11, 2} inicializa o array com valores inteiros específicos
sizeof(numbers) / sizeof(numbers[0]) calcula o número total de elementos no array
-
Adicione uma instrução de impressão para verificar o tamanho do array:
#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 e execute o programa para verificar o tamanho do array.
gcc even_numbers.c -o even_numbers
./even_numbers
Exemplo de saída:
Array size: 10