C Arrays

What is an Array?

An array is a collection of variables of the same type stored in a single block of memory.

Arrays allow you to store multiple values using one variable name.

Loading...
Output:

Declaring and Initializing Arrays

You can declare an array and initialize it at the same time.

The number inside brackets defines the size of the array.

Loading...
Output:

Accessing Elements

Array elements are accessed using an index.

Indexing starts at 0.

Loading...
Output:

Modifying Elements

You can change values in an array by assigning a new value to an index.

Loading...
Output:

Looping Through Arrays

Arrays are commonly used with loops to process multiple values.

Loading...
Output:

Array Size

You can calculate the number of elements using sizeof().

Loading...
Output:

Memory Layout

Arrays are stored in contiguous memory locations.

This means each element is stored next to the previous one.

Loading...
Output:

Arrays and Pointers

In C, arrays are closely related to pointers.

The array name represents the address of the first element.

Loading...
Output:

Multidimensional Arrays

Arrays can have multiple dimensions, like a table or matrix.

Loading...
Output:

Common Mistakes

Be careful with these:

  • Accessing out-of-bounds indexes
  • Forgetting array size
  • Confusing arrays with pointers

Why Arrays are Important

Arrays are fundamental in C and are used in almost every program.

  • Store multiple values efficiently
  • Used with loops and algorithms
  • Foundation for strings and data structures

Practice

Create an array of integers and print all elements using a loop.

Loading...
Output:

Need Help?