C Traversing Arrays

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Traversing Arrays?

Traversing means visiting each element to print, sum, search, or transform it. The standard pattern is a for loop from 0 to n - 1. You can also iterate backwards or walk a pointer from the first element to one past the last.

Remember
for (int i = 0; i < n; i++)  { use(a[i]); }   /* forward  */
for (int i = n - 1; i >= 0; i--) { use(a[i]); }   /* backward */
for (int *p = a; p < a + n; p++) { use(*p); }     /* pointer  */

Cheat Sheet

Keep these facts handy while you work with Traversing Arrays in C.

TopicDetail
Forwardi = 0; i < n; i++
Backwardi = n - 1; i >= 0; i-- (use signed i)
Pointerp < a + n
Common jobsSum, max, count, search

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Two-dimensional Arrays.

Example 1 — Forward, backward, and maximum

Three traversals over one array.

C
#include <stdio.h>

int main(void) {
    int a[] = {3, 8, 1, 9, 4};
    int n = sizeof a / sizeof a[0];
    int max = a[0], at = 0;

    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    printf("\n");
    for (int i = n - 1; i >= 0; i--) printf("%d ", a[i]);
    printf("\n");
    for (int i = 1; i < n; i++) {
        if (a[i] > max) { max = a[i]; at = i; }
    }
    printf("max=%d at index %d\n", max, at);
    return 0;
}

How It Works

  1. The first loop prints in order, the second in reverse.
  2. The maximum search starts from index 1 because a[0] seeds max.
  3. Use a signed int for reverse loops; an unsigned counter would wrap around.

Edge Cases & Pitfalls

Check these before relying on Traversing Arrays in real programs.

unsigned

Unsigned reverse loop

for (size_t i = n - 1; i >= 0; i--) never ends because i >= 0 is always true.

len

Hard-coded lengths

Compute n once from sizeof so the loop survives array changes.

Key Takeaways

  • Pattern: i < n for forward loops.
  • Reverse: use a signed counter.
  • Seed: initialize max/min from the first element.

One line: Traversing means visiting each element to print, sum, search, or transform it.

Frequently Asked Questions

Use for (int i = 0; i < n; i++) and access a[i]. Calculate n as sizeof a / sizeof a[0] when a is a real array in the same scope.
Yes. Start with p = a and stop when p reaches a + n.
Continue with "C Two-dimensional Arrays" next. Store tables of rows and columns with a[row][col]. The C Arrays hub lists every topic in order.

Next: Two-dimensional Arrays

Store tables of rows and columns with a[row][col].

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.

11 people found this page helpful