You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'In the C language, an array is a data structure of a fixed length that stores a list of values.'
3
+
Description: 'An array in C is a data structure that allows users to store a fixed-size sequence of elements of the same data type.'
4
4
Subjects:
5
5
- 'Code Foundations'
6
6
- 'Computer Science'
7
7
Tags:
8
8
- 'Arrays'
9
9
- 'Lists'
10
+
- 'Values'
10
11
- 'Variable Types'
11
12
CatalogContent:
12
13
- 'learn-c'
13
14
- 'paths/computer-science'
14
15
---
15
16
16
-
In the C language, an array is a list of values, with a fixed length.
17
+
An **array** in C is a data structure that enables users to store a fixed-size sequence of elements of the same [data type](https://www.codecademy.com/resources/docs/c/data-types). Arrays are useful when there is a need to work with collections of data, such as lists of numbers, characters, or other values.
17
18
18
-
Being able to store multiple pieces of related information in the same structure is very useful when writing C programs.
19
+
**Key Characteristics:**
19
20
20
-
## Declaring an Array
21
+
-**Fixed Size**: Once declared, the size of the array cannot be changed.
22
+
-**Homogeneous Elements**: All array elements must be of the same data type (e.g., `int`, `float`, etc.).
23
+
-**Indexed Access**: Elements are accessed using zero-based indexing (the first element is at index `0`).
24
+
-**Contiguous Memory Allocation**: Array items are stored in consecutive memory locations, which makes accessing them efficient.
25
+
-**Efficient Iteration**: Arrays can be easily traversed using loops, allowing users to perform operations on each element.
26
+
-**No Bounds Checking**: C does not perform automatic bounds checking, so accessing out-of-range elements may lead to undefined behavior.
21
27
22
-
The syntax for declaring an array is first specify the data type, then a descriptive array name, followed by square brackets surrounding the array's length (number of items):
28
+
## Declaring and Initializing an Array
29
+
30
+
Here is the syntax for declaring an array in C:
23
31
24
32
```pseudo
25
33
type name[length];
26
34
```
27
35
28
-
To declare an `int` array named `grades` with a length of 6:
36
+
In the syntax:
37
+
38
+
-`type`: The data type of the elements to be stored in the array.
39
+
-`name`: The name of the array.
40
+
-`length`: The length or the number of elements to be stored in the array.
41
+
42
+
This example declares an `int` array named `grades` with a length of `6`:
29
43
30
44
```c
31
-
int grades[6];// An array to hold six integers
45
+
int grades[6];
32
46
```
33
47
34
-
Alternatively, the length can be omitted and the array's initial values can be assigned to it instead. Values are assigned inside of the curly brackets and separated by commas.
48
+
Though an array can be initialized after declaration, it is generally initialized during declaration. Here is the syntax for it:
49
+
50
+
```pseudo
51
+
type name[length] = {value1, value2, ..., valueN};
52
+
```
53
+
54
+
In the syntax:
55
+
56
+
-`value1, value2, ..., valueN`: The elements to be stored in the array.
57
+
58
+
This example declares and intializes the `grades` array with values:
35
59
36
60
```c
37
61
int grades[] = {96, 90, 78, 84, 88, 92};
38
62
```
39
63
40
-
## Accessing Values with Indexes
64
+
> **Note:** When arrays are initalized during declaration, the length is generally omitted.
41
65
42
-
The values in arrays are accessed using their index, or their position in the array. They can either be assigned or used this way.
66
+
## Accessing Array Elements
67
+
68
+
The values in arrays are accessed using their index or position in the array.
69
+
70
+
For example, here is an array:
43
71
44
72
```c
45
73
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
46
74
```
47
75
48
-
Note that in C, an array's indexes start at 0 instead of 1:
76
+
The index chart for the array will look like this:
49
77
50
-
```pseudo
78
+
```plaintext
51
79
+-------------------------+
52
80
Value | 'a' 'e' 'i' 'o' 'u' |
53
81
+-------------------------+
54
82
Index | 0 1 2 3 4 |
55
83
+-------------------------+
56
84
```
57
85
86
+
> **Note:** In C, an array's indices start at `0` instead of `1`.
87
+
58
88
An element can be accessed by referring to the array name and the element's index number:
59
89
60
90
-`vowels[0]` will give the value `'a'`
@@ -63,34 +93,125 @@ An element can be accessed by referring to the array name and the element's inde
63
93
-`vowels[3]` will give the value `'o'`
64
94
-`vowels[4]` will give the value `'u'`
65
95
96
+
Here is an example:
97
+
66
98
```c
67
-
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
99
+
#include<stdio.h>
100
+
101
+
intmain() {
102
+
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
103
+
104
+
putchar(vowels[3]);
68
105
69
-
putchar(vowels[3]);
70
-
// Output: o
106
+
return 0;
107
+
}
71
108
```
72
109
73
-
## Example
110
+
The output will be:
74
111
75
-
Creating an array that holds the snowfall measurements (in the nearest inch) from the past 7 days:
112
+
```shell
113
+
o
114
+
```
115
+
116
+
## Modifying Array Elements
117
+
118
+
The items of an array can be modified by directly assigning values to specific indices:
76
119
77
120
```c
78
121
#include<stdio.h>
79
122
80
123
intmain() {
81
-
int snowfall[] = {10, 13, 14, 11, 9, 8, 6};
82
-
printf("%d\n", snowfall[0]);
124
+
int numbers[3] = {1, 2, 3};
125
+
126
+
numbers[0] = 10;
83
127
84
-
snowfall[2] = 16; // Update an element
85
-
printf("%d\n", snowfall[2]);
128
+
for (int i = 0; i < 3; i++) {
129
+
printf("%d\n", numbers[i]);
130
+
}
86
131
87
132
return 0;
88
133
}
89
134
```
90
135
91
-
The output would be:
136
+
Here is the output:
92
137
93
138
```shell
94
139
10
95
-
16
140
+
2
141
+
3
96
142
```
143
+
144
+
## Iterating an Array
145
+
146
+
Arrays in C can be traversed using loops such as `for`, `while`, or `do-while`. The `for`[loop](https://www.codecademy.com/resources/docs/c/loops) is most commonly used for this purpose:
147
+
148
+
```c
149
+
#include<stdio.h>
150
+
151
+
intmain() {
152
+
int numbers[] = {5, 10, 15, 20, 25};
153
+
int i;
154
+
155
+
for (i = 0; i < 5; i++) {
156
+
printf("%d\n", numbers[i]);
157
+
}
158
+
159
+
return 0;
160
+
}
161
+
```
162
+
163
+
Here is the output:
164
+
165
+
```shell
166
+
5
167
+
10
168
+
15
169
+
20
170
+
25
171
+
```
172
+
173
+
## Example: Basic Array Operations
174
+
175
+
Here's a complete example demonstrating array declaration, initialization, element modification, and iteration:
176
+
177
+
```c
178
+
#include<stdio.h>
179
+
180
+
intmain() {
181
+
int marks[5] = {80, 85, 90, 75, 95};
182
+
183
+
// Modify one element
184
+
marks[2] = 100;
185
+
186
+
// Print all elements
187
+
for (int i = 0; i < 5; i++) {
188
+
printf("Student %d: %d marks\n", i + 1, marks[i]);
189
+
}
190
+
191
+
return 0;
192
+
}
193
+
```
194
+
195
+
Here is the output:
196
+
197
+
```shell
198
+
Student 1: 80 marks
199
+
Student 2: 85 marks
200
+
Student 3: 100 marks
201
+
Student 4: 75 marks
202
+
Student 5: 95 marks
203
+
```
204
+
205
+
## Frequently Asked Questions
206
+
207
+
### 1. Can arrays in C store elements of different data types?
208
+
209
+
No. arrays in C are homogeneous, meaning all elements must be of the same data type.
210
+
211
+
### 2. Is it possible to change the size of an array after declaration?
212
+
213
+
No. The size of an array is fixed once declared. For dynamic sizes, consider using dynamic memory allocation with `malloc()`.
214
+
215
+
### 3. What happens if I access an element out of the array's bounds?
216
+
217
+
Accessing out-of-bounds elements in an array leads to undefined behavior and can cause unpredictable results or crashes.
0 commit comments