TL;DR: Arrays in C help programmers store, access, and process related data efficiently through indexing, loops, contiguous memory, and pointer behavior.

Arrays in C are one of the most fundamental tools for managing data collections. Whether you’re storing a list of integers, managing a sequence of characters, or working with complex data structures, arrays provide an efficient way to handle data systematically. Arrays allow us to store and manage multiple elements of the same data type using a single variable. Each value can be accessed using an index. 

In this guide, we cover the definition and declaration, initialization, memory layout, basic operations, and pointer behavior to help you implement them effectively in your programs.

What Is an Array in C?

An array in C is a collection of elements of the same data type stored in contiguous memory locations. Each element is accessed using the array name and an index. These entities or elements can be int, float, char, double, or user-defined data types, like structures. They are beneficial for tasks like managing lists, performing mathematical operations on datasets, and handling data sequences in loops.

  • Elements can be accessed directly using their indices, and operations can be performed on multiple elements using loops
  • They require a fixed size and only store the same data type
  • If the size is omitted in the declaration, it must be determined at initialization

AI-Powered Full Stack Developer ProgramExplore Program
Get the Coding Skills You Need to Succeed

How to Declare, Initialize, and Access Arrays in C

To declare or define an array in C, you must specify the data type, the array name, and the size. For beginner examples, the array size is usually written as a positive integer constant. C also has variable-length arrays in some standards, and fixed-size arrays are clearer for portable beginner code. The general syntax for declaring an array is as follows.

data_type array_name[array_size];
int numbers[5];
float prices[10];
char name[20];

In this example:

  • data_type: Specifies the type of elements the array will hold (e.g., int, float, char).
  • array_name: The identifier for the array.
  • array_size: The number of elements the array can hold (must be a positive integer).

Array Initialization

Array initialization is the process of assigning values to the elements of an array at the time of declaration. You can initialize arrays either partially or fully using curly braces {}.

The initialization syntax is:

data_type array_name[array_size] = {value1, value2, ..., valueN};

Full Initialization

You can choose to fully initialize the array by assigning a starting value to every allocated index.

int numbers[5] = {1, 2, 3, 4, 5};

Here, the numbers array has 5 elements, each assigned a specific value.

Partial Initialization

In partial initialization, the compiler pads any leftover indices by initializing them with zeros if you provide a partially empty array at the start.

int numbers[5] = {1, 2};

Implicit Size Determination

Leaving the size parameter blank allows the compiler to count the items you provide and set the total length automatically.

int numbers[] = {1, 2, 3};

In the example above, the array size is 3.

Character Array Initialization

Character arrays and strings in C are explained simply by initializing an array using a string literal. The array stores the characters followed by the null terminator \0.

char name[] = "Alice";

Array Access

Array elements can be accessed using their index values. The index starts at 0, so the first element is at position 0, the second at position 1, and so on. For size n, the last index is n − 1. To use arrays safely, you need to understand both indexing and array bounds. 

  • C does not automatically stop you from using an invalid index. 
  • Accessing outside the array bounds causes undefined behavior.

Example:

#include <stdio.h>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};  // Declare and initialize an array
    // Accessing elements
    printf("First element: %d\n", arr[0]);
    printf("Third element: %d\n", arr[2]);
    return 0;

Output

First element: 10

Third element: 30

Learn 45+ in-demand full-stack development skills and tools, including Frontend Development, Backend Development, Version Control and Collaboration, Database Management, and AI Assisted Development, with our AI-Powered Full Stack Developer Course

Types of Arrays in C

Arrays in C are commonly grouped by the number of indices required to access an element. A one-dimensional array uses one index, and a multidimensional array uses two or more indices.

One-Dimensional Arrays

A one-dimensional array is the simplest form of an array, storing elements in a single linear sequence. It can be visualized as a row of elements. 

  • Use a one-dimensional array when the data can be represented as a list.
  • Setting this up requires declaring the specific data format, a custom identifier, and the desired total length.

Declaration:

data_type array_name[size];

Example:

int arr[5]; // Declares an array of size 5

Initialization:

int arr[5] = {1, 2, 3, 4, 5};

Accessing Elements

printf("%d", arr[2]); // Accesses the third element, which is 3

  • Storing a list of numbers, such as marks or salaries.
  • Simple linear data handling.

Multidimensional Arrays

Multidimensional arrays are arrays within arrays, allowing data to be stored in tabular or matrix form. A 2D array organizes data in rows and columns, making it ideal for matrix representation.

  • You declare it by providing the data type, the name, the number of rows, and the number of columns.
  • Declaring an array[2][3] creates a 2 × 3 array with 2 rows and 3 columns.

Declaration:

data_type array_name[rows][columns];

Example:

int arr[2][3]; // Declares a 2D array with 3 rows and 4 columns

Initialization:

int arr[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

This matrix has 2 rows, each with 3 integers. You access an element with two indexes, one for the row and one for the column.

Points to remember while initializing a 2D Array:

  • The total number of elements must not exceed rows * columns.
  • Omitted elements are automatically initialized to 0.
  • Use nested braces for better readability.

C also supports arrays with more than two dimensions, and two-dimensional arrays serve as the most common beginner example. Understanding one-dimensional vs multidimensional arrays in C helps you choose the right structure for your data.

How Arrays Are Stored in Memory

Arrays are stored contiguously, which means the elements are placed next to each other in memory. This layout is why index-based access is efficient, as the next elements follow in order of the data type's size. Also, C stores multidimensional arrays in row-major order. 

  • In a 2 × 3 array, the first row is stored first, followed by the second row. 
  • For a matrix, the memory order places the first row elements sequentially before moving on to the second row. 
  • As a result, the latest element is accessed and changed fastest.

AI-Powered Full Stack Developer ProgramEXPLORE COURSE
Advance Your Full Stack Career!

Basic Array Operations in C

You can perform array operations in C with practical examples focusing on input and output, summing values, reversing values, and finding the maximum element.

Input and Output Array Elements

Taking user input for an array allows dynamic data entry. You can run a loop to capture integers, and another to output the collected elements.

Example:

#include <stdio.h>
int main() {
    int arr[3];
    printf("Enter 3 integers: ");
    for (int i = 0; i < 3; i++) {
        scanf("%d", &arr[i]);
    }
    printf("You entered: ");
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Sum of Array Elements

You can find the sum of all elements by initializing a sum variable to 0, looping through the array, and adding each element. This cumulative addition pattern is highly common in data analysis tasks.

#include <stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int sum = 0;
    int size = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    printf("Sum: %d\n", sum);
    return 0;
}

The expression sizeof arr divided by sizeof arr 0 gives the number of elements in the array in the same scope where the array is declared. This specific size-calculation technique loses its effectiveness once the data block enters a separate function space. Avoiding common array-related errors in C programming requires careful attention to loop boundaries and initialization steps.

Find the Maximum Element in an Array

You can find the maximum element by assuming the first element is the maximum and comparing it with the remaining elements.

#include <stdio.h>
int main() {
    int arr[] = {3, 8, 1, 6, 7};
    int max = arr[0];
    int size = sizeof(arr) / sizeof(arr[0]);
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    printf("Maximum: %d\n", max);
    return 0;
}

Passing Arrays to Functions in C

In C, arrays can be passed to functions to perform operations on them. Since arrays are pointers to the first element, they are passed by reference, meaning the function can modify the original array. 

  • Passing arrays to functions in C with examples demonstrates that the function can modify the original array elements, and it does not automatically know the array length.
  • Always pass the size as a separate parameter unless the function has another reliable way to know where the sequence ends.

Example:

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    printArray(arr, size);
    return 0;
}

Here:

  • The array arr is passed to the printArray function.
  • The array size is a separate parameter since C doesn't track array sizes.

C does not allow returning an entire array directly, but you can:

  • Return a pointer to the array.
  • Use dynamic memory allocation (e.g., with malloc).
  • Pass the array to the function by reference and modify it.
Wondering how Software Engineers reach senior and leadership roles? Explore the skills, technologies, salary growth, and career progression behind one of the world's most in-demand jobs with this software engineer roadmap.

Conclusion

Arrays in C help you store, access, and manage data efficiently using contiguous memory, direct indexing, loops, pointers, and function passing. Once you understand declaration, initialization, memory layout, and pointer behavior, you can use arrays confidently in real-world programs and data structures. If you’re ready to take your coding skills to the next level, enroll in our AI-Powered Full Stack Developer Course to build practical programming and full-stack development skills.

FAQs 

1. What is the difference between arrays and pointers in C?

An array is a fixed-size collection of elements stored in contiguous memory locations. A pointer is a variable that stores the address of another variable or memory location. In many expressions, an array name behaves like a pointer to its first element, but arrays and pointers are not the same. Arrays have a fixed size, while pointers can be reassigned to different memory locations.

2. What are common array operations in C?

Common array operations in C include declaring an array, initializing values, accessing elements using indexes, taking input, printing elements, finding the sum, searching for a value, finding the maximum or minimum element, reversing elements, and sorting data. Most array operations use loops because arrays store related values in a sequence.

3. What are the advantages of arrays in C?

Arrays make it easier to store and manage multiple values of the same data type using a single variable. They support fast access through indexes, work well with loops, and use contiguous memory, which makes data handling efficient. Arrays are useful for lists, tables, matrices, strings, and many data structure operations.

Our Software Development Program Duration and Fees

Software Development programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Full Stack Development Program with Generative AI

Cohort Starts: 29 Jun, 2026

20 weeks$4,000