Introduction to Arrays
Sometimes you need to create hundreds or even thousands of variables. Creating them one by one isn't practical. That's where arrays come in. An array is a collection of variables of the same type. If a single variable is like one storage box, an array is a warehouse filled with boxes, each holding its own value. Declaring an array looks something like this:
int array[3];
Here's how you declare an array with space for three elements. To assign values to it, use curly braces to list them inside.
int array[3] = {1, 5, 10};
int array[] = {56, 3, 10};
If you specify the items directly, you don't need to declare the size, the compiler automatically counts and assigns the number of elements.
Indexes
Each box in an array has its own unique identifier, called an index, which lets you easily access specific elements. An index is the number assigned to each item in the array, similar to your position in line at a coffee shop. Indexes in C start at zero, meaning the first element has an index of 0.
Main.c
12345678910#include <stdio.h> int main() { // Array declaration int array[3] = {56, 3, 10}; // Display the first element of the array printf("%d", array[0]); return 0; }
The arrays discussed so far are static, meaning their size stays fixed during the program’s execution. There are also dynamic arrays, which can change size while the program is running.
You can change the value of any element in an array by referring to its specific index.
main.c
123456789101112131415161718#include <stdio.h> int main() { int array[3] = { 56, 3, 10 }; printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); array[1] = 555; // change 10 to 555 printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); return 0; }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 2.63
Introduction to Arrays
Veeg om het menu te tonen
Sometimes you need to create hundreds or even thousands of variables. Creating them one by one isn't practical. That's where arrays come in. An array is a collection of variables of the same type. If a single variable is like one storage box, an array is a warehouse filled with boxes, each holding its own value. Declaring an array looks something like this:
int array[3];
Here's how you declare an array with space for three elements. To assign values to it, use curly braces to list them inside.
int array[3] = {1, 5, 10};
int array[] = {56, 3, 10};
If you specify the items directly, you don't need to declare the size, the compiler automatically counts and assigns the number of elements.
Indexes
Each box in an array has its own unique identifier, called an index, which lets you easily access specific elements. An index is the number assigned to each item in the array, similar to your position in line at a coffee shop. Indexes in C start at zero, meaning the first element has an index of 0.
Main.c
12345678910#include <stdio.h> int main() { // Array declaration int array[3] = {56, 3, 10}; // Display the first element of the array printf("%d", array[0]); return 0; }
The arrays discussed so far are static, meaning their size stays fixed during the program’s execution. There are also dynamic arrays, which can change size while the program is running.
You can change the value of any element in an array by referring to its specific index.
main.c
123456789101112131415161718#include <stdio.h> int main() { int array[3] = { 56, 3, 10 }; printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); array[1] = 555; // change 10 to 555 printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); return 0; }
Bedankt voor je feedback!