Types of Array In C [One Dimensional Array, Two Dimensional Array, and Multi-Dimensional Array In C]

Hello friends, In today’s article we are going to talk about types of array (One Dimensional Array, Two Dimensional Array, and Multi-Dimensional Array).

Today we will learn in detail about what is 1D Array? What is 2D Array? What is Multi-Dimensional Array? And what is the difference between 1One Dimensional Array and Two Dimensional Array?

But first, you need to know what is array and why an array is used.

If you already know about Array then read directly about the types of Array and if you do not know anything about Array then first of all you should read the article given below.

Before knowing about the types of array in c, you need to have some basic knowledge about array, So for now let’s understand what is array?

Whenever we create a variable in C language, then we can store only one value in that variable at a time.

If we have to store many values ​​together in a group of variables, then we cannot do this work by creating normal variables because it will take a lot of time to store values ​​by creating normal different variables.

For example – If we want to store the roll number of 100 students of a class in a group, then first we have to think 100 variable names and then declare those variables and store the roll numbers in them.

In this whole process, first of all, it will take time to think the name of the variable and also we will not be able to easily find out which variable is storing the roll number of which student.

But C language provides us another functionality to do all these things which we call Array.

An array is a group of variables in which the data type of all the variables present is the same. Every array element has a different index number by which we can easily access that array element or variable.

By declaring an array, we can declare many variables inside it, its declaration is something like this -:

Syntax -:

data_type array_name[size];

Example -: int a[10];

In this example, we have created a group of 10 variables by single array declaration.

Let’s now learn about the types of array in c 

Types of Array In C [One Dimensional Array, Two Dimensional Array, and Multi-Dimensional Array In C]

Types of Array in C 

There are mainly three types of arrays in C language -:

  1. One – dimensional arrays (1D Array)
  2. Two – dimensional arrays (2D Array)
  3. Multi – dimensional arrays

Let’s understand about all these types of arrays one by one.

1. One Dimensional Array in C (1D Array)

The normal array we make in C language is a one-dimensional array. Its syntax is something like this -:

Syntax -:

data_type array_name[array_size];

datatype – Indicates what kind of data is going to be stored in the array.

array_name – Here we write the name of the array so that we can access and use the array with this name anywhere in our program.

array_size – Here we write the size of array, suppose if I want to make array of 20 variables then I will write 20 here and if I want to make array of 200 variables then here I will write 200.

Example -:

int number[100];

int is the data type, number is the name of the array, and 100 is the size of that array.

Note -:

When the array is declared, the garbage value is stored in it by default.

The index number of the array by default starts from zero, like if the size of the array is 5 then its index number will be something like arr[0], arr[1], arr[2], arr[3], arr[4] |

Initialization of one Dimensional Array In C

In C language we can initialize an array in different ways -:

  1. Compile time initialization
  2. Runtime initialization

1. Compile-time initialization of a One Dimensional Array (1D Array In C)

Just as we initialize a normal variable in C language, we can also initialize 1D Array in the same way and when we do this, it is called Compile-time Initialization.

When this program is compiled, then at the same time the value of this array will be initialized.

Examples -:

int var[10] = {10,20,30,40,50,60,70,80,90,100};
int var2[] = {101,201,301,401,501,601,701,801,901,};
int var3[10] = {11,22,33,44,55};

2. Runtime initialization of a One Dimensional Array

When in Array, the value is initialized at program run time then it is called run time initialization.

Example -:

#include<stdio.h>
void main()
{
    int arr[4];
    int i, j;
    printf("Enter array element : ");
    for(i = 0; i < 4; i++)
    {
        scanf("%d", &arr[i]);    //Run time array initialization
    }
    printf("Printing array element : ");
    for(j = 0; j < 4; j++)
    {
        printf("%d\n", arr[j]);
    }
}

In this example, we took the values ​​from the user and stored them in the array element, and later printed those values ​​in the screen with the help of printf() function.

Output -:

Enter array element : 21
23
32
43
Printing array element :
21
23
32
43

Accessing Elements of One Dimensional Array In C

We can access the array element by array name and index number and can modify them accordingly.

Example -:

#include<stdio.h>
void main()
{   int i;
    int arr[5] =  {10,20,30,40,50};
    printf("Before any changes result :");
    for(i = 0; i < 5; i++)
    {
     printf("%d ", arr[i]);
    }

    arr[3] = 45;
    printf("\nAfter changes result : ");
    for(i = 0; i < 5; i++)
    {
        printf("%d ",arr[i]);
    }
}

Output

Before any changes result : 10 20 30 40 50
After changes result : 10 20 30 45 50

In this example, we changed the value of the element at the array’s arr[3] index by doing arr[3] = 45

Example Program of One Dimensional Array In C

#include<stdio.h>
void main()
{
    int arr[5], i;
    for(i = 0; i < 5; i++)
    {
        printf("Enter a[%d]: ", i);
        scanf("%d", &arr[i]);
    } 

    printf("\nPrinting elements of the array: \n\n");

    for(i = 0; i < 5; i++)
    {
        printf("%d ", arr[i]);
    }

    // signal to operating system program ran fine
}

Output -:

Enter a[0]: 21
Enter a[1]: 22
Enter a[2]: 423
Enter a[3]: 321
Enter a[4]: 343

Printing elements of the array:

21 22 423 321 343

Let’s now understand what is Two Dimensional Array In C and how it works?

2. Two Dimensional Array in C (2D Array)

Two Dimensions Array is like a Matrix. Which is represented in the form of rows and columns. In simple words, it is also called Array of Array. Its declaration is something like this -:

Syntax -:

data_type array_name[rows][columns];

Example -:

int arr[21][30];

Initialization of Two Dimensional Array In C(2D Array)

In C language we can initialize two Dimensional Array in these 2 ways -:

  1. Compile time initialization
  2. Runtime initialization

1. Compile-time Initialization of a Two Dimensional Array In C

In compile-time initialization, we initialize two dimensional array like this -:

int matrix_A[2][3] = {
                        {1, 2, 3},
                        {4, 5, 6}

2. Runtime Initialization of a Two Dimensional Array In C

When the value of Two Dimensional Array is initialized at run time then it is called Runtime initialization.

Example -:

#include<stdio.h>
void main()
{
    int arr[4][4];
    int i, j, k;
    printf("Enter array element");
    for(i = 0; i <= 3;i++)
    {
        for(j = 0; j < 4; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
}

Accessing Individual Elements of Two Dimensional Array In C

#include<stdio.h>
void main()
{
    int arr[3][4];
    int i, j, k;
    printf("Enter array element");
    for(i = 0; i < 3;i++)
    {
        for(j = 0; j < 4; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
  printf(“Accessing Individual Elements of Two Dimensional Array : \n”);
  for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 4; j++)
        {
            printf("%d", arr[i][j]);
        }
    }
}

3. Multidimensional Array in C

In C language, we call Multidimensional array in simple words array of arrays, in which data is stored in tabular form (in the form of rows and columns).

Syntax -:

data_type array_name[size1][size2]....[sizeN];

Here the data type tells what type of data will be held in the array element.

Example -:

int three_dArray[2][3][4];

Initializing Three Dimensional Arrays In C

int three_dArray[2][3][4] = {
    {{3, 4, 5, 7}, {1, -3, 10, 11}, {3, 12, 53, 20}},
    {{17, 4, 16, 3}, {56, 9, 31, 5}, {30, 1, 42, 9}}};

Example of Three Dimensional Arrays In C

#include <stdio.h>
int main()
{ int x,y,z;
  int arr[2][3][2] = {
      { {1,-5},{2,-6}, {3,-7} },
      { {4,-8},{5,-8}, {6,-10} }
  };
  for ( x=0;x<2;x++)
  {
    for ( y=0;y<3;y++)
    {
       for ( z=0;z<2;z++)
       {
        printf("%d  ", arr[x][y][z]);
       }
       printf("\n");
    }
  printf("\n");
  }
  return 0;
}

Output -:

1 -5
2 -6
3 -7

4 -8
5 -9
6 -10

Difference Between One Dimensional Array and Two Dimensional Array In C

One Dimensional Array Two Dimensional Array
A One Dimensional array is a group of variables in which each element/variable has the same data type.Each element in a two-dimensional array is itself a 1D array, so it is also called an array of arrays.
There is no concept of rows and columns in One Dimensional Array.There is a concept of rows and columns in Two Dimensional Array.
Syntax – :data_type array_name[array_size];  Syntax -:data_type array_name[rows][columns];  
Example: int a[15];Example: int a[5][3];

Conclusion

Friends, I hope that after reading this article, you all must have got the information that how many types of array in c language ? And what is a 1D array? What is 2D Array? What is Multi-Dimensional Array? And what are the difference between one dimensional array and two dimensional array?

If you want a complete tutorial of C language then see this post C Language Tutorial, here you will get all the topics of C Programming  step by step

Friends, I hope that you have liked this post and you must have got a lot of knowledge about the types of arrays in c.

If you liked this post, then do not forget to share this post with your friends, so that they can also get this information.

If you still have any question or Doubts related to the types of array (One Dimensional Array, Two Dimensional Array and Multi Dimensional Array) then you tell me, I will answer all your questions and for more information, you can contact us. |

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.