To initialize an array in C, you can use array literals.
In this example,
numbers
with integer values using array literals.#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {
printf("%d ", numbers[i]);
}
return 0;
}
1 2 3 4 5
In this example,
fruits
with character values using array literals.#include <stdio.h>
int main() {
char fruits[] = {'A', 'p', 'p', 'l', 'e'};
for (int i = 0; i < sizeof(fruits) / sizeof(fruits[0]); i++) {
printf("%c ", fruits[i]);
}
return 0;
}
A p p l e
In this tutorial, we learned How to Initialize an Array in C language with well detailed examples.