C Arrays

Beginner
15 min read
Updated: Nov 2026
1 example
9 guides

Definition and Usage

An array stores a fixed number of values of the same type in contiguous memory, accessed by a zero-based index. This section covers declaring and initializing arrays, traversing them, two-dimensional and multidimensional arrays, variable length arrays (VLAs), passing arrays to functions, and the everyday operations of searching, inserting, and reversing.

Arrays do not know their length.

C does not store or check an array's size. You must track the length yourself and keep every index in the range 0 to n - 1.

Learning Path

Work through the stages in order, or jump to a topic from the index below (9 tutorials).

C Examples

A quick taste of arrays. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Average of an array

Declare, initialize, loop, and compute.

C
#include <stdio.h>

int main(void) {
    int scores[] = {85, 90, 78, 92, 88};
    int n = sizeof scores / sizeof scores[0];
    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += scores[i];
    }
    printf("n=%d sum=%d average=%.1f\n", n, sum, (double)sum / n);
    return 0;
}

How It Works

  1. The compiler infers the size 5 from the initializer.
  2. sizeof scores / sizeof scores[0] computes the element count.
  3. The cast keeps the average fractional: 433 / 5 = 86.6.

C Arrays Topics

Every guide in this section with a one-line summary (9 tutorials).

One-dimensional arrays

4 tutorials

Create, fill, read, and loop over arrays.

TopicDescriptionTutorial
Array basicsUnderstand what an array is, how it sits in memory, and why indexes start at 0.Try it
Declaration and initializationDeclare arrays with a size and initialize them with braces or designated indexes.Try it
Accessing array elementsRead and write elements with a[i], and see how it relates to pointer arithmetic.Try it
Traversing arraysVisit every element with for loops, reverse loops, and pointer walks.Try it

Multidimensional arrays

3 tutorials

Tables, grids, and higher dimensions, plus VLAs.

TopicDescriptionTutorial
Two-dimensional arraysStore tables of rows and columns with a[row][col].Try it
Multidimensional arraysGo beyond two dimensions: 3D arrays, indexing, and memory layout.Try it
Variable length arraysC99 arrays whose size is decided at run time.Try it

Working with arrays

2 tutorials

Pass arrays to functions and write common routines.

TopicDescriptionTutorial
Arrays and functionsPass arrays and 2D arrays to functions, and fill arrays from functions.Try it
Common array operationsReverse, search, insert, and delete elements in an array.Try it

Edge Cases & Pitfalls

Common mistakes when learning arrays in C.

bounds

Out-of-bounds access

a[5] on a 5-element array is undefined behavior and is never reported by the compiler. Loop with i < n.

size

Wrong length for parameters

sizeof inside a function gives the pointer size, not the array size.

assign

Assigning whole arrays

b = a; is illegal for arrays. Copy with a loop or memcpy.

Key Takeaways

  • Zero-based: valid indexes are 0 to n - 1.
  • Contiguous: elements sit side by side in memory.
  • Length: track it yourself.

One line: An array stores a fixed number of values of the same type in contiguous memory, accessed by a zero-based index.

Frequently Asked Questions

Write the element type, name, and size in brackets, for example int scores[5];. You can initialize it with braces: int scores[] = {1, 2, 3};.
Zero. An array of n elements has indexes 0 through n - 1.
No. All elements share one type. Use a struct to combine different types.

Next: Array Basics

Understand what an array is, how it sits in memory, and why indexes start at 0.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful