c
  1. c-1-d-array

C 1-D Array

Syntax

datatype array_name[array_size];

Example

#include <stdio.h>

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

  printf("%d\n", myArray[2]);

  return 0;
}

Output

The output of the above example will be 3.

Explanation

An array in C is a collection of elements of the same data type, accessed using an index or subscript. The array is created by specifying the data type, the name of the array, and the number of elements in the array. In the example above, the myArray array is defined as an integer array with 5 elements. The elements are initialized using curly braces { }.

Use

Arrays are used in C to store and manipulate multiple related values. They provide a convenient way of representing data that has a natural structure, such as lists or tables. Arrays are commonly used in algorithms and data structures, as well as in scientific and mathematical programs.

Important Points

  • The first index of an array in C is always 0.
  • Array elements can be accessed using their index, which is enclosed in square brackets []. For example, myArray[2] would access the third element of the myArray array.
  • Arrays in C are static, meaning their size is fixed at compile time and cannot be changed at runtime.
  • The size of an array in C can be determined using the sizeof() function.

Summary

C 1-D arrays are a fundamental feature of the C programming language. They provide a convenient way of storing and manipulating related values. Understanding the syntax and usage of arrays is important for anyone learning C, as they are used extensively throughout the language. It is important to keep in mind that arrays are static and their size cannot be changed at runtime, so proper initialization and bounds checking are important to prevent runtime errors.

Published on: