Arrays in C Language: Definition, Initialization, and Traversal

Arrays in C Language: Definition, Initialization, and Traversal

In the C language, an array is a collection used to store a fixed number of elements of the same type. Arrays allow us to conveniently handle large amounts of data and are a very practical data structure. In this article, we will detail how to define, initialize, and traverse arrays.

1. Definition of Arrays

The basic syntax for defining an array in C is as follows:

type array_name[size];
  • type: Represents the data type of the elements stored in the array, which can be a basic data type (such as <span>int</span>, <span>float</span>, etc.).
  • array_name: Assigns a name to the array.
  • size: Indicates the number of elements the array can store.

Example

#include <stdio.h>
int main() {    int numbers[5]; // Define an array that can hold 5 integers    return 0;}

In the code above, we defined an integer array <span>numbers</span>, which can store five integer values.

2. Initialization of Arrays

We can assign initial values to variables at the time of declaration or initialize them separately. Here are two common initialization methods.

1. Static Initialization

Static initialization directly assigns values to each element at the time of declaration, ensuring that each element has a defined initial value. The syntax is as follows:

type array_name[size] = {element1, element2, ..., elementN};

If fewer initial values than the specified size are provided, the uninitialized positions will automatically be set to 0.

Example

#include <stdio.h>
int main() {    int numbers[5] = {1, 2, 3}; // The first three positions are assigned 1, 2, and 3, the remaining two default to 0
    for (int i = 0; i < 5; i++) {        printf("%d ", numbers[i]);    }
    return 0;}

Running the above code will output:

1 2 3 0 0 

2. Dynamic Allocation and Partial Initialization

It is also possible to not specify a specific size and let the size be automatically inferred from the provided number of elements:

Example

#include <stdio.h>
int main() {    int numbers[] = {4, 5, -1, -8}; // Size inferred from the provided number of elements
    for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {        printf("%d ", numbers[i]);    }
    return 0;}

The output will be:

4 5 -1 -8 

3. Traversing Arrays

To access and process each element, we need to loop through the entire array. Typically, a <span>for</span> loop is used to accomplish this, allowing access to each item via its index.

Example Code

Below is a complete example that includes definition, initialization, and traversal operations:

#include <stdio.h>
int main() {    int grades[10] = {90, 85, 92, 78, 88, 76, 95, 89, 84, 91}; // Initialize grades for ten subjects
    printf("Grades:\n");
    // Traverse and print each subject's score    for (int i = 0; i < sizeof(grades) / sizeof(grades[0]); i++) {        printf("Subject %d: %d\n", i + 1, grades[i]);
        // Use a conditional check to determine if passed (assuming passing mark is 60)        if(grades[i] >= 60) {            printf("Subject %d is Passed.\n", i + 1);        } else {            printf("Subject %d is Failed.\n", i + 1);        }
    }
    return 0;
}

After running the above program, you will get the following output (depending on the specific grades):

For example:

Grades:Subject 1: 90Subject 1 is Passed....Subject 10: 91  Subject x is Passed.

4. Conclusion

This article introduced the basics of the C language, including how to define and initialize static and dynamic-sized arrays, as well as how to effectively traverse these arrays to access various data. Mastering these concepts is crucial for future programming practice, and we hope everyone can practice more and continuously improve their programming skills.

Leave a Comment