In C programming, handling a large number of variables can be challenging, especially when they are of the same type. Consider the following example:

main() {
    int a1, a2, a3, ..., a50;
    printf("Enter 50 values: ");
    scanf("%d%d...%d", &a1, &a2, ..., &a50);
    printf("%d%d...%d", a1, a2, ..., a50);
}

This approach, where individual variables are used to store each value, is cumbersome and inefficient. To overcome this, C provides the concept of Arrays.

What is an Array?

An array is a collection of variables of the same type, stored in contiguous memory locations. This allows for efficient memory usage and easy manipulation of large amounts of data. Arrays in C use zero-based indexing, meaning the first element is accessed with index 0, the second with index 1, and so on.

Syntax:

datatype arrayname[size];

For example:

int a[50];

Key Points about Arrays:

  • An array is a collection of homogeneous elements, meaning all elements are of the same data type.
  • It allows storing multiple values under a single variable name.
  • Array elements are stored in contiguous memory locations.
  • Indexing starts from 0 and goes up to size-1.
  • Before using an array, its data type and size must be declared.
  • Arrays are allocated memory during runtime, and uninitialized elements contain garbage values by default.

Memory Allocation for Arrays

Let’s see how memory is allocated to arrays in C with some examples:

main() {
    int a[5];
    int b[2+3];
    int c[5/2];
    int d[0]; // Not valid in most C compilers
    int e[2.5]; // Not valid
    int f['A']; // Valid, 'A' is interpreted as its ASCII value
    int g[65];
    int h[-1]; // Not valid
    int x = 10;
    int i[x]; // Not valid in C89, valid in C99
    int j[sqrt(16)]; // Not valid
    int k = sqrt(16);
    int l[k]; // Not valid in C89, valid in C99
}

Initializing Arrays

Arrays can be initialized in several ways:

Type 1: Inline Initialization

main() {
    int a[3] = {1, 2, 3};
    printf("%d %d %d", a[0], a[1], a[2]);
}

Output: 1 2 3

Note: If fewer elements are provided during initialization, the remaining elements will be set to 0.

Type 2: Individual Assignment

main() {
    int a[3];
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    printf("%d %d %d", a[0], a[1], a[2]);
}

Type 3: User Input

main() {
    int a[3];
    printf("Enter 3 elements: ");
    scanf("%d %d %d", &a[0], &a[1], &a[2]);
    printf("%d %d %d", a[0], a[1], a[2]);
}

Basic Programs on Arrays

1. Input and Output

main() {
    int a[100], n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    printf("Enter %d elements: ", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; i++) {
        printf("%d\n", a[i]);
    }
}

2. Average of Numbers

main() {
    int a[100], i, n, sum = 0;
    float avg;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    printf("Enter %d elements: ", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; i++) {
        sum += a[i];
    }
    avg = (float)sum / n;
    printf("Average = %f", avg);
}

3. Find Minimum and Maximum

main() {
    int a[100], i, n, big, small;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    printf("Enter %d elements: ", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    big = small = a[0];
    for(i = 1; i < n; i++) {
        if(a[i] > big) big = a[i];
        if(a[i] < small) small = a[i];
    }
    printf("Max = %d, Min = %d", big, small);
}

    Thank you for reading! Your thoughts and suggestions are always welcome—let’s connect in the comments below!