C Programming Tutorial
About Lesson

C Array

In this tutorial, we will learn about Arrays and its type in C programming and also learn how to declare, initialize and access elements of an array with the help of examples.


Array

An array is a variable which is used to store a collection of similar type of data items stored at contiguous memory locations .
We can also access an element randomly using their indices.

Syntax :

Datatype Array_name[Array_size] = { element1, element2, ...........};

Example :

int num[10] = {1,2,3,4,5,6,7,8,9,0};
int marks[] = {11,12,13,14,15,16,17,18,19,20};

In the above example, we have declared an integer type array num[10] having size of 10 i.e. it can store maximum 10 integer values.


Why do we need Arrays?

Consider the problem of storing 10 integers. The naive way to store these integers would be to create 10 different integers variables and store 1 integer in each of them. This would be a viable solution but manipulating these variables would be a task in itself and we would have to keep track of their names always. Thus, not an efficient solution for a programmer.

So we introduce the concept of arrays. An array is a data structure that stores a fixed collection of variables of the same type. They are always present in the memory as contiguous blocks of data. Basically, you can create one variable who has different variables on its indexes. Indexes tell the memory location on which a variable is stored.


How to Declare an Array in C?

The general structure of an array declaration is:

dataType arrayName[arraySize];

Note: The array size has to be an integer.

When an array is declared, depending on the compiler used, the initial values of the array may be 0s or garbage values as well. Thus, it is often good practice to initialize the array with some values.
Examples:

int arr[10] = {0}; //Simple Declaration with all elements 0
double price[5]; // Any data type allowed, including structs
long int phoneNumber[] = {100, 101, 102}; //Implicit size declaration

Points to Remember :

  1. It is compulsory to declare the size and type of array.
  2. If we declare and initialize an array together then it is not necessary to declare the size of the array.
  3. All the elements must be written inside curly brackets {} and seperated with comma (,).
  4. Indexing in an array is always starts from 0.

Note : It’s important to note that the size and type of an array cannot be changed once it is declared.


Advantages of Arrays

  • Less Code to write : Array make our code well optimized and easy to read /understand.
  • Random Access of elements : We can easily access any element randomly of array.
  • Easy sorting : We can easily sort elements of array with few lines of code only.
  • Easy Transversing : We can easily retrive elements of an array by using loops.
  • Multi-dimensional arrays : It allows us to store the elements in any dimensional array.

Access Array Elements

Elements of an array can be accessed by indexing the the array name.
We have to place the index of the element within square brackets after the name of the array.

Example :

int result = marks[5]; 

In the above example, we have take the 6th elements from the array and assign it in variable result of int type.

C Array Declaration
Elements of an array in C

Example :

//Program to print an element of an array 
#include <stdio.h>
void main() {
  int num[5] = {3,2,4,6,7};
  printf("%d ", num[3]);
  printf("%d ", num[11]);  //Gives an error
}

Output :

6 32664

In the above example, We have declared and initialize an array num of int type.
Then we have access the element having index 3 Then we have get 6 as result but we have tried to access the element having index 11, which is out of bound of array num then we get a random number as result.


Change Value of Array Elements

//Program to change value of element of an array 
#include <stdio.h>
void main() {
  int num[5] = {1, 2, 3, 4, 5};
  //assign a new value to the fourth element of array
  num[3] = 10;
  //assign a new value to the first element of array
  num[0] = 9;
  for(int b = 0; b < 5; b++) {
    printf("%d ", num[b]);  // Printing the elements of the arrays
  }
}

Output :

9 2 3 10 5

Note : We can only change one element of an array at a time.


Input and Output Array Elements

// Program to take Input of array elements and give output
#include <stdio.h>
int main() {
  int a, b, num[5];
  printf("Enter a integer : ");
  for(a = 0; a < 5; a++) {
    scanf("%d", &num[a]);
  }
  printf("Elements in the array : ");
  for(b = 0; b < 5; b++) {
    printf("%d ", num[b]);
  }
  return 0;
}

Output :

Enter a integer : 2
5
6
1
9
Elements in the array : 2 5 6 1 9

Example :

// Program to print an individual element of an array
#include <stdio.h>
int main() {
  int num[5] = {1, 2, 3, 4, 5};
  printf("%d ", num[0]);// print the first element of the array
  printf("%d ", num[2]);// print third element of the array
  return 0;
}
error: Content is protected !!