C Arrays

An array in C is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow us to store multiple values under a single variable name, making it easier to manage and manipulate data. Each element in an array is accessed using an index, which starts from 0.

In this tutorial, we will learn about the basics of an array, properties of an array, and present a list of tutorials related to arrays.


Basic Example of an Array

Let’s declare and initialize an integer array and print its elements.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize an integer array
    int numbers[] = {10, 20, 30, 40, 50};
    
    // Print elements of the array
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with five elements.
  2. We use a for loop to iterate through the array, starting from index 0 and ending at 4.
  3. The printf() function prints each element of the array.

Output:

10 20 30 40 50

Properties of an Array

  1. Fixed Size: The size of an array is fixed at the time of declaration and cannot be changed dynamically.
  2. Contiguous Memory Allocation: Array elements are stored in consecutive memory locations.
  3. Same Data Type: All elements in an array must be of the same data type (e.g., int, float, char).
  4. Indexing: Array elements are accessed using indices starting from 0.
  5. Efficient Access: Direct access to any element using its index provides fast retrieval.

C Arrays Tutorials

The following tutorials cover basics of an Array like initialization, iterating over elements of array, array properties, etc.

1. Basic Array Operations

  1. How to Initialize an Array in C
  2. How to Print an Array in C
  3. How to Declare and Initialize an Integer Array in C
  4. How to Declare and Initialize a Character Array (String) in C
  5. How to Find the Length of an Array in C
  6. How to Copy One Array to Another in C
  7. How to Reverse an Array in C
  8. How to Merge Two Arrays in C
  9. How to Compare Two Arrays in C
  10. How to Check if an Array is Sorted in C
  11. How to Convert a Character Array into a String in C
  12. How to Iterate over Array using While Loop in C
  13. How to Iterate over Array using For Loop in C

2. Array Searching

  1. How to Check if an Array Contains Specific Element in C
  2. How to Search for an Element in an Array (Linear Search) in C
  3. How to Perform Binary Search on a Sorted Array in C
  4. How to Search for Multiple Occurrences of an Element in an Array in C
  5. How to Find the Index of the First Occurrence of an Element in C
  6. How to Find the Index of the Last Occurrence of an Element in C
  7. How to Find the Frequency of Elements in an Array in C

3. Array Sorting

  1. How to Sort an Array using Bubble Sort in C
  2. How to Sort an Array using Selection Sort in C
  3. How to Sort an Array using Insertion Sort in C
  4. How to Sort an Array using Quick Sort in C
  5. How to Sort an Array using Merge Sort in C

4. Mathematical Operations on Arrays

  1. How to Find the Sum of All Elements in an Array in C
  2. How to Find the Average of Array Elements in C
  3. How to Find the Maximum Value in an Array using Loops in C
  4. How to Find the Minimum Value in an Array using Loops in C
  5. How to Find the Second Largest Element in Array in C
  6. How to Find the Second Smallest Element in Array in C
  7. How to Find the Sum of Even and Odd Elements Separately in an Array in C
  8. How to Count Positive and Negative Numbers in an Array in C
  9. How to Calculate the Product of All Elements in an Array in C

5. String Arrays (Character Arrays)

  1. How to Read a String Array in C
  2. How to Print a String Array in C
  3. How to Copy a String to Another using Character Arrays in C
  4. How to Compare Two Strings using Character Arrays in C
  5. How to Reverse a String Using a Character Array in C
  6. How to Find the Length of a String without using strlen() in C
  7. How to Concatenate Two Strings using Character Arrays in C
  8. How to Convert Uppercase Letters to Lowercase in a Character Array in C
  9. How to Convert Lowercase Letters to Uppercase in a Character Array in C
  10. How to Count the Number of Vowels and Consonants in a Char Array in C
  11. How to Remove White Spaces from a Character Array in C

6. 2D and Multi-Dimensional Arrays

  1. How to Declare and Initialize a 2D Array in C
  2. How to Read a 2D Array in C
  3. How to Print a 2D Array in C
  4. How to Transpose a 2D Array (Matrix) in C
  5. How to Multiply Two Matrices using 2D Arrays in C
  6. How to Find the Sum of All Elements in a 2D Array in C
  7. How to Find the Row-wise and Column-wise Sum in a 2D Array in C
  8. How to Find the Largest Element in a 2D Array in C
  9. How to Find the Smallest Element in a 2D Array in C
  10. How to Perform Matrix Addition using Arrays in C
  11. How to Perform Matrix Subtraction using Arrays in C
  12. How to Rotate a 2D Array (Matrix) in C
  13. How to Flatten a 2D Array into a 1D Array in C

7. Advanced Array Topics

  1. How to Use Pointers with Arrays in C
  2. How to Pass an Array to a Function in C
  3. How to Return an Array from a Function in C

Conclusion

In this C Tutorial, we learned about different concepts of Arrays in C programming with dedicated tutorials for each of the concepts.