What Is an Array? - C Array

C examples for Array:Introduction

Introduction

An array is a fixed number of data items of the same type.

The data items in an array are referred to as elements.

The following code is an array declaration.

long numbers[10];

The number between square brackets defines how many elements the array contains and is called the array dimension.

Array index values are sequential integers that start from zero.

0 is the index value for the first array element.

For the following array:

long numbers[10];

The index values for the elements in the numbers array run from 0 to 9.

The index value 0 refers to the first element and the index value 9 refers to the last element.

You access the elements in the numbers array as numbers[0], numbers[1], numbers[2], and so on, up to numbers[9].

Demo Code

#include <stdio.h>

int main(void)
{
  int grades[10];                          // Array storing 10 values
  unsigned int count = 10;                 // Number of values to be read

  printf("\nEnter the 10 grades:\n");      // Prompt for the input

  // Read the ten numbers
  for(unsigned int i = 0 ; i < count ; ++i)
  {//from   ww w .j  av  a 2  s.c om
    printf("%2u> ",i + 1);
    scanf("%d", &grades[i]);               // Read a grade
  }
  for(unsigned int i = 0 ; i < count ; ++i)
    printf("\nGrade Number %2u is %3d", i + 1, grades[i]);

  return 0;
}

Result


Related Tutorials