§ — — Computer Programming 2
Suppose your program needs to work with 20 integers — reading them, processing them, and printing them. One approach is to declare 20 separate variables, each with its own name. That works for a small number, but it quickly becomes unmanageable. Imagine doing the same thing for 200, 2000, or 20,000 values. You'd need a separate variable declaration and a separate line of code for every single one of them.
Arrays solve this problem. Instead of giving each value its own name, an array groups many values of the same data type under a single name, and lets you refer to individual values by their position (called an index or subscript).
An array has three defining properties:
Because elements are stored in sequence, you can refer to them by position: the first element, the second element, and so on. In C, positions are counted starting from zero. So for an array named num with 20 elements, the valid positions are num[0] through num[19].
The notation num[i] — a name followed by an integer expression in square brackets — is called index notation. The value inside the brackets is the index (or subscript), and it tells the compiler which element you want. Using a variable like i as the index is what makes it possible to process entire arrays with loops.
Before an array can be used it must be declared. The declaration tells the compiler three things: the name of the array, the data type of its elements, and how many elements it holds.
General form for a one-dimensional array:
data_type array_name[size];
data_type — the type of each element (int, float, char, double, etc.)array_name — any valid C identifiersize — a positive integer constant specifying the number of elementsExamples:
int scores[20]; /* 20-element integer array */
char labels[30]; /* 30-element character array */
float data[20]; /* 20-element floating-point array */
double values[12]; /* 12-element double array */
The storage class is optional and can be placed before the data type. If omitted, the default depends on where the array is declared (automatic inside a function, external outside).
The array name itself is a symbolic reference to the address of the first element in memory. To locate any specific element, C uses this formula:
element address = base address + (size of one element × index)
For example, if an integer array starts at memory address 10,000 and each integer occupies 2 bytes, the element at index 3 is at:
10,000 + (2 × 3) = 10,006
This is why array indexing is fast — calculating an address from a base and an offset is a single arithmetic operation.
Declaring an array only reserves memory — it does not put any values in. There are three ways to get values into an array.
You can supply initial values in curly braces at the point of declaration:
/* General form */
data_type array_name[size] = {value1, value2, ..., valueN};
Rules:
Examples:
int a[5] = {5, 3, 2, 7, 9}; /* all 5 elements initialized */
int b[] = {11, 21, 75, 24, 5}; /* size inferred as 5 */
int c[15] = {3, 7, 4, 6, 1}; /* first 5 set; elements 5–14 = 0 */
Use a loop combined with scanf() to fill an array at runtime:
int scores[10];
int i;
for (i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
}
Note that &scores[i] uses the address-of operator — required by scanf() just as it is for ordinary variables.
You can assign a value to any single element using the assignment operator:
scores[4] = 23;
You cannot assign one array directly to another (e.g., second = first; is illegal). To copy an array you must loop through it element by element:
for (i = 0; i < 25; i++)
second[i] = first[i];
A string in C is stored as a character array. The key difference from other arrays is the null terminator: every string ends with the special character '\0', which the compiler adds automatically when you initialize with a string literal.
char city[6] = "HELLO";
This creates a 6-element array: 'H', 'E', 'L', 'L', 'O', '\0'. You can also let the compiler determine the size:
char city[] = "HELLO"; /* compiler sets size to 6 automatically */
If you declare the array without an initial value and enter the size yourself, make sure to add one extra slot for the null terminator.
C supports arrays with more than one dimension. A two-dimensional array is the most common and can be thought of as a table with rows and columns.
Declaration:
data_type array_name[rows][columns];
Examples:
float grid[5][3]; /* 5 rows, 3 columns of floats */
int table[3][3]; /* 3×3 integer table */
Values are assigned row by row (the last subscript increases fastest):
int mat[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
This produces:
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
You can also use nested braces to make the row grouping explicit:
int mat[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
If a row's inner brace contains fewer values than the column count, the remaining elements in that row are set to zero. However, you cannot put more values in a row's inner brace than the declared column count — that is a compile error.
This program sorts a small integer array using a basic comparison-and-swap approach.
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int num[3] = {5, 3, 7};
int h, i, temp;
for (h = 0; h < 3; ++h)
for (i = 0; i < h; ++i) {
if (num[i] > num[i + 1]) {
temp = num[i];
num[i] = num[i + 1];
num[i+1] = temp;
}
}
for (i = 0; i < 3; i++)
printf("%d\n", num[i]);
getch();
}
Expected output:
3
5
7
Free Sample
That was 1 of 12 reviewers with answer keys in Computer Programming 2. Unlock all of them for the semester.
Unlock all reviewers →ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Done with this module? Track it — your progress shows on the subject list.
Up next
Exam Prep: Prelims & Finals→