2 Dimensional Array in C Programming Language
By rajkishor09
We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array (1D array). The 2D array is also known as Matrix or Table, it is an array of array. See the 2D array image, in that image each row is an array.
Declaration of 2D array:
Syntax: data_type array_name[row_size][column_size];
Example: int arr[3][3];
So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).
2D array image
Memory Map of 2D Array
![]() | Amazon Price: $23.94 List Price: $30.00 |
![]() | Amazon Price: $31.38 List Price: $39.99 |
![]() | Amazon Price: $32.00 List Price: $64.99 |
![]() | Amazon Price: $30.94 List Price: $39.99 |
![]() | Amazon Price: $49.00 List Price: $74.20 |
Code for assigning & displaying 2D Array
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int arr[3][3]={
{12, 45, 63},
{89, 34, 73},
{19, 76, 49}
};
clrscr();
printf(":::2D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
Displaying 2D array using for loop.
So in the above example we have declared a 2D array named arr which can hold 3x3 elements. We have also initialized that array with values, because we told the compiler that this array will contain 3 rows (0 to 2) so we divided elements accordingly. Elements for column have been differentiated by a comma (,). When compiler finds comma in array elements then it assumes comma as beginning of next element value. We can also define the same array in other ways, like.
int arr[3][3]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][3]={12, 45, 63, 89, 34, 73, 19, 76, 49};
But this kind of declaration is not acceptable in C language programming.
int arr[2][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49};
To display 2D array elements we have to just point out which element value we want to display. In our example we have a arr[3][3], so the array element reference will be from arr[0][0] to arr[2][2]. We can print display any element from this range. But in our example I have used for loop for my convenience, otherwise I had to write 9 printf statements to display all elements of array. So for loop i handles row of 2D array and for loop j handles column. I have formatted the output display of array so that we can see the elements in tabular form.
Visitor's Opinion
Is this tutorial on 2D-Array clear enough to understand?
See results without votingC programming tutorial links
- C Programming Lesson - Arrays
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int, float, and char. - String in C Programming Language
String in C is very interesting topic to learn. We all know about arrays but here we put that in good use. Even you can character (char) pointer for string. C has lots of inbuilt functions which can reverse a string, can get length of string etc. In - C Programming Lesson - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different type - Swap two variables without using third variable in C , C# and Java
One of my friends (visitor) asked me in comment section “how to swap two variables without using temp variable” and I thought of sharing that answer with all my friends (visitors). For the first I am also going to provide full code in Java and C#. - C Programming Lesson - Do While Loop and Difference Between While and Do While Loop
If you are looking for what is loop and why we use loop in c language then you can check my article on that topic. This article is entirely dedicated to do-while loop and its usage. I must tell you that do-while loop is similar to while loop but only - C Programming - Time in C
Time in C is example of C program which displays current system time or digital clock and it’s dynamic clock which will keep it updated (synchronized) with system time. o understand this program you should have knowledge of structure, library functio - C Programming Lesson - Multidimensional array (3D Array)
C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array having two dimensions called 2D array and so on. - C Programming Lesson - Function in C
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program So - Call by Value and Call by Reference in C Programming
Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to
Useful Tutorials
- Best Free PHP Tools & Editors for Every PHP Programmer
PHP (recursive acronym for: PHP Hypertext Preprocessor), is a widely-used, Open Source embedded scripting language. PHP is a server-side scripting language usually written in an HTML context. Unlike an ... - How to enable “Show Hidden Files and Folders” option disabled after virus attack
If you ever faced such problem when your computer had a virus which ultimately disabled “show hidden file and folders” option, then you don’t need to format or restore your setting. I have a trick... - Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you don’t need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you... - How to setup network between XP and Vista PCs
Vista is Microsofts new operating system and it provides different network and security method than Windows XP, to its user. So in order to create network between Vista and XP, we have to enable few...
very good site
i got my all problems clear
thanks... i can use it on my class...
I have clear idea when i was seen this site. Thank you
Way too much ads crap. Impossible to read normally.
Nice...Thank you.
hi frnds i got some idea about arrays after seeing tat
i want merging two dimensional arrays in c language
Thank you friend for written data type.
yesterday i was completely confuse when i was writing 2d array prog but now now my concept is totally clear about it
is there any difference between these two assignment?
int arr[ ][3]={12, 45, 63, 89, 34, 73, 19, 76, 49};
and
int arr[3 ][]={12, 45, 63, 89, 34, 73, 19, 76, 49};
compiler shows second one is wrong..y?
excellent
not bad
C is very easy to learn
it is awesome
GOOD
its not enough,,abut array more explanation shud be there,,
it is not easy making array statement without following the example given by the instructor or else the teacher
Write a program that creates a two-dimensional array with 10 rows and 2 columns. The first column should be filled with 10 random numbers between 0 and 100. The second column should contain the squared value of the element found in column 1. Using the show ( ) method of the MessageBox class, display a table. Please someone help me out on this program?????
super
nice,very good thank you
write program of maximum and minimum using twodemension array using c
very useful
write aprogram read the integar 10 elements into an array and display it reverse order?
aaaaaa shutttte









bucles 2 years ago
nice-1!