Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Introduction to Arrays | Data
C Basics

bookIntroduction 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};
Note
Note

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

Main.c

copy
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; }
Note
Note

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

main.c

copy
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; }
question mark

Given the following array declaration in C, what value will be printed by the code below?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show me how to access a specific element in an array?

How do I change the value of an element in an array?

What happens if I try to access an index that doesn't exist?

Awesome!

Completion rate improved to 2.63

bookIntroduction to Arrays

Svep för att visa menyn

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};
Note
Note

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

Main.c

copy
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; }
Note
Note

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

main.c

copy
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; }
question mark

Given the following array declaration in C, what value will be printed by the code below?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
some-alt