English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structures

C Language Files

C Others

C Language Reference Manual

C Language Arrays

In this tutorial, you will learn how to use arrays. You will learn how to declare, initialize, and access array elements through examples.

An array is a variable that can store multiple values. For example, if you want to store10If you need to store 0 integers, you can create an array for it.

int data[100];

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

In this case, we declare a float type array mark. Its size is5. This means it can hold5floating-point values.

It should be noted thatThe size and type of an array cannot be changed once declared.

Accessing array elements

You can access the elements of the array by index.

Assuming you have declared an array mark as described above. The first element is mark[0], the second element is mark[1], and so on.

Description:

  • The first index of the array is 0, not1. In this example, the first element of the array is mark[0].

  • If the size of the array is n, then to access the last element, n-1then use the index. In this example, the first element of the array is mark[4]

  • Assuming the starting address mark[0] is2120d. Then, the address mark[1] will be2124d. Similarly, the address mark[2] will be2128detc.
    This is because the size of a is float4bytes.

How to initialize an array?

Arrays can be initialized during declaration. For example,

int mark[5]= {19, 10, 8, 17, 9};

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

In this case, we have not specified the size. However, when we use5an element is initialized, the compiler knows its size is5.

Here,

mark[0] equals 19
mark[1] equals 10
mark[2] equals 8
mark[3] equals 17
mark[4] equals 9

to change the value of array elements

int mark[5]= {19, 10, 8, 17, 9}
//to set the value of the third element to-1
mark[2]= -1;
//to set the value of the fifth element to 0
mark[4]= 0;

Input and output array elements

This is the method of obtaining input from users and storing it in array elements.

// Get input and store it in the third element
scanf("%d", &mark[2]);
// Store the input in the ith element
scanf("%d", &mark[i]);-1]);

This is the method of printing a single element of the array.

//Print the first element of the array
printf("%d", mark[0]);
//Print the third element of the array
printf("%d", mark[2]);
//Print the ith element of the array
printf("%d", mark[i]);-1]);

Example1: Array input/Output

//The program gets5values and store them in the array
//Print the elements stored in the array
#include <stdio.h>
int main() {
  int values[5];
  printf("Input5integers: ");
  //Accept input and store it in the array
  for(int i = 0; i < 5; ++i) {
     scanf("%d", &values[i]);
  }
  printf("Display integer: ");
  //Print the elements of the array
  for(int i = 0; i < 5; ++i) {
     printf("%d\n", values[i]);
  }
  return 0;
}

Output result

Input5integers: 1
-3
34
0
3
Display integer: 1
-3
34
0
3

Here, we use a for loop to get5of the entered values and store them in an array. Then, using another for loop, these elements are displayed on the screen.

Example2: Calculate average

//The program uses an array to find the average of n numbers
#include <stdio.h>
int main()
{
     int marks[10], i, n, sum = 0, average;
     printf("Enter the number of elements: ");
     scanf("%d", &n);
     for(i=0; i< ++i)
     {
          printf("Enter number %d: ", i+1);
          scanf("%d", &marks[i]);
          
          //Add the integer entered by the user to the sum variable
          sum += marks[i]
     }
     average = sum/n;
     printf("Average = %d", average);
     return 0;
}

Output result

Enter the number of elements: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

Here, we calculate the average of n numbers entered by the user.

Accessing element out of range!

Suppose you declare an array of10elements composed array. For example

int testArray[10];

You can access the array element testArray[0] - testArray[9].

Now, suppose you try to access testArray[12]. This element is not available. This may cause unexpected output (throw an error).

Therefore, never access elements outside the array boundaries.

Multidimensional Arrays

In this tutorial, you have learned about arrays. These arrays are called one-dimensional arrays.

In the next tutorial, you will learnMultidimensional Arrays (Arrays of Arrays).