c
  1. c-array-test

C Array Test

Syntax

data_type array_name[array_size];

Example

#include <stdio.h>

int main()
{
    int myArray[5] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++) {
        printf("Value at index %d: %d\n", i, myArray[i]);
    }

    return 0;
}

Output

The output of the above program will be:

Value at index 0: 1
Value at index 1: 2
Value at index 2: 3
Value at index 3: 4
Value index 4: 5

Explanation

An array is a collection of elements of a single data type that are referenced by a common name. The syntax for declaring an array in C language is data_type array_name[array_size];. In the above example, int is the data type, myArray is the name of the array, and 5 is the size of the array.

The elements of an array can be accessed by their index, starting from 0 to size-1. In the above example, myArray[0] refers to the first element of the array, myArray[1] refers to the second element of the array, and so on.

Use

Arrays are used to store a collection of data that can be easily accessed and manipulated. They can be used to store data of any primitive data type such as int, float, char, etc. Arrays are commonly used in sorting algorithms, searching algorithms, and data storage applications.

Important Points

  • The size of an array cannot be changed once it is declared.
  • An array index must be an integer value.
  • Array elements are stored in contiguous memory locations.
  • Arrays can only store elements of a single data type.

Summary

C language provides an easy-to-use array data structure that is capable of storing a collection of elements of a single data type. Arrays are commonly used in sorting and searching algorithms as well as data storage applications. The syntax for declaring an array in C language is simple and easy to understand. Understanding the basic syntax and use cases for the C array data structure is an essential component of any C programming course.

Published on: