C Arrays


Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].

To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};

We have now created a variable that holds an array of four integers.


Access the Elements of an Array

To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

This statement accesses the value of the first element [0] in myNumbers:

Example

int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

// Outputs 25
Try it Yourself »

Change an Array Element

To change the value of a specific element, refer to the index number:

Example

myNumbers[0] = 33;

Example

int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25
Try it Yourself »

Loop Through an Array

You can loop through the array elements with the for loop.

The following example outputs all elements in the myNumbers array:

Example

int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf("%d\n", myNumbers[i]);
}
Try it Yourself »

Set Array Size

Another common way to create arrays, is to specify the size of the array, and add elements later:

Example

// Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Try it Yourself »

Using this method, you should know the number of array elements in advance, in order for the program to store enough memory.

You are not able to change the size of the array after creation.


Get Array Size or Length

To get the size of an array, you can use the sizeof operator:

Example

int myNumbers[] = {10, 25, 50, 75, 100};
printf("%lu", sizeof(myNumbers)); // Prints 20
Try it Yourself »

Why did the result show 20 instead of 5, when the array contains 5 elements?

- It is because the sizeof operator returns the size of a type in bytes.

You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.

Knowing the memory size of an array is great when you are working with larger programs that require good memory management.

But when you just want to find out how many elements an array has, you can use the following formula (which divides the size of the array by the size of one array element):

Example

int myNumbers[] = {10, 25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);

printf("%d", length);  // Prints 5
Try it Yourself »

Making Better Loops

In the array loops section above, we wrote the size of the array in the loop condition (i < 4). This is not ideal, since it will only work for arrays of a specified size.

However, by using the sizeof formula from the example above, we can now make loops that work for arrays of any size, which is more sustainable.

Instead of writing:

Example

int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf("%d\n", myNumbers[i]);
}
Try it Yourself »

It is better to write:

Example

int myNumbers[] = {25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
int i;

for (i = 0; i < length; i++) {
  printf("%d\n", myNumbers[i]);
}
Try it Yourself »

Real-Life Example

To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;
int i;

// Get the length of the array
int length = sizeof(ages) / sizeof(ages[0]);

// Loop through the elements of the array
for (int i = 0; i < length; i++) {
  sum += ages[i];
}

// Calculate the average by dividing the sum by the length
avg = sum / length;

// Print the average
printf("The average age is: %.2f", avg);
Try it Yourself »

C Exercises

Test Yourself With Exercises

Exercise:

Create an array of type int called myNumbers.

   {25, 50, 75, 100};

Start the Exercise



Copyright 1999-2023 by Refsnes Data. All Rights Reserved.