Back to C Programming
2026-07-145 min read

Array (C Programming)

Learn Array (C Programming) step by step with clear examples and exercises.

Why This Matters

Learning arrays is essential for understanding and mastering C programming. In this lesson, we'll delve into what arrays are, how they work, and common mistakes to avoid when using them. By the end of this tutorial, you'll be able to create, manipulate, and debug array-based programs with confidence.

Prerequisites

Before diving into arrays, it's important that you have a solid understanding of the following C concepts:

  1. Basic syntax (variables, operators, expressions)
  2. Data types (int, char, float)
  3. Functions and function prototypes
  4. Input/output functions (printf, scanf)

Core Concept

An array in C is a collection of variables of the same data type stored at contiguous memory locations. Each variable within an array is called an element, and arrays are zero-indexed, meaning the first element has an index of 0.

To declare an array, you specify its name, data type, and size enclosed in square brackets []. For example:

int myArray[5]; // Declares an array named 'myArray' with 5 elements of type int

You can initialize the values of an array during declaration using curly braces {}. Here's an example:

int myArray[] = {1, 2, 3, 4, 5}; // Declares and initializes an array named 'myArray' with 5 elements of type int

To access an element of an array, you use its name followed by the index in square brackets. For example:

int myArray[] = {1, 2, 3, 4, 5};
printf("%d", myArray[0]); // Outputs: 1

You can also change the value of an element using the same syntax. Here's an example:

int myArray[] = {1, 2, 3, 4, 5};
myArray[0] = 10; // Changes the first element to 10
printf("%d", myArray[0]); // Outputs: 10

Worked Example

Let's create a simple program that takes an array of integers as input, calculates their sum, and outputs the result.

#include <stdio.h>

int main() {
int numElements, i;
int myArray[10]; // Declares an array with 10 elements of type int

printf("Enter the number of elements in the array: ");
scanf("%d", &numElements);

if(numElements > 10) {
printf("Error: Array size exceeds 10 elements.\n");
return 1; // Exits the program with an error code
}

printf("Enter %d integers:\n", numElements);
for(i = 0; i < numElements; ++i) {
scanf("%d", &myArray[i]);
}

int sum = 0;
for(i = 0; i < numElements; ++i) {
sum += myArray[i];
}

printf("The sum of the elements is: %d\n", sum);
return 0; // Exits the program successfully
}

Save this code in a file named array_sum.c. To compile and run it, open your terminal, navigate to the directory containing the file, and execute the following commands:

gcc array_sum.c -o array_sum
./array_sum

You'll be prompted to enter the number of elements in the array and their values. After entering the input, you should see the sum of the elements displayed on your screen.

Common Mistakes

  1. Forgetting to include the square brackets when declaring an array:
int myArray; // Incorrect declaration
  1. Accessing an element outside its valid range:
int myArray[5];
printf("%d", myArray[-1]); // Outputs: garbage value (undefined behavior)
printf("%d", myArray[5]); // Outputs: garbage value (undefined behavior)
  1. Using the wrong data type for an array:
char myArray[2] = {0, 1}; // Incorrect initialization; char arrays can only hold 1 byte each
  1. Forgetting to allocate memory for dynamic arrays:
int numElements;
scanf("%d", &numElements);
int *myArray = (int*)malloc(numElements * sizeof(int)); // Correct allocation
// ...
free(myArray); // Don't forget to free the allocated memory when done!
  1. Mixing up array and pointer notation:
int myArray[3] = {1, 2, 3};
int *p = &myArray; // Correct pointer declaration
printf("%d", p); // Outputs garbage value (pointer address)
printf("%d", *p); // Correctly outputs the first element of the array: 1

Practice Questions

  1. Write a program that finds the maximum and minimum values in an array of integers.
  2. Create a program that reverses the order of elements in an array.
  3. Write a function that takes two arrays as input, compares their elements, and returns the index of the first mismatch or -1 if they are identical.
  4. Implement a function that sorts an array of integers using bubble sort.
  5. Write a program that calculates the average of the elements in an array and prints the result with two decimal places.

FAQ

  1. Why is it important to initialize arrays when declaring them?

Initializing arrays at declaration allows you to set default values for each element, which can be useful for debugging or testing purposes. If you don't initialize an array, all its elements will have indeterminate (garbage) values.

  1. Can I declare a multidimensional array in C?

Yes! A multidimensional array is simply an array of arrays. You can declare a 2D array like this: int myArray[3][4];. This creates a 3x4 grid of integers.

  1. How do I find the size of an array in C?

In C, the size of an array is determined at compile time and cannot be changed. To get the size of an array, you can use the sizeof operator. For example: int myArray[10]; has a size of 40 bytes (assuming a 32-bit system).

  1. What happens if I access an element outside the valid range of an array?

Accessing an element outside the valid range of an array results in undefined behavior, which can lead to memory corruption or segmentation faults. It's important to always check your indices and avoid going out of bounds.

  1. Can I use a pointer to access elements of an array?

Yes! Pointers are powerful tools that allow you to manipulate arrays more flexibly. By using the address-of operator (&) and dereference operator (*), you can treat an array as a contiguous block of memory and access its elements directly through pointers.