C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

C language Arrays tutorial

This is a tutorial on Arrays in C. The word array lets one define and declare a set of variables, data type of the variable can be anything from the available data types like int, char, float, double etc. The arrays can be defined as below:

int Firstarray[10];

In the above declaration the value 10 is known as subscript and these subscripts are the number of element in any array. Here we have 10 elements.

These elements can be accessed by using the appropriate index value as a subscript. A small program will help this understand better.

#include <stdio.h>

int main()

	int Firstarray[10]; //array declaration
	char index; //loop counter

	Firstarray[0]= 1;  //first element is on index 0.
	Firstarray[1]= 2;  //for second element subscript is 1.
	Firstarray[2]= 3;	//subscript or the index is n-1, where n is the size of your array.
	Firstarray[3]= 4;	//in this case size is 10
	Firstarray[4]= 5;
	Firstarray[5]= 6;
	Firstarray[6]= 7;
	Firstarray[7]= 8;
	Firstarray[8]= 9;
	Firstarray[9]= 10;


	return 0;



If you observe in the above code, that is how we use the arrays. There are several ways available to do so. When we declare a array[10], the actual elements are array[0] to array[9]. In C, 0 is valid and so it is also a valid address and index as well.

The array above is a one dimensional array. We can assume that after executing the code above we will have something like below in the memory.

1  2  3  4  5  6  7  8  9  10

All the variables in the array are allocated with a contiguous memory location means they are next to each other.

Most common usage is to store a group of data of same kind. Try different programs to explore more about the Arrays.

The above was a brief on single dimensional array. These arrays can also be of 2 dimensions. Like we will have two subscripts. Below is the example.

int SecondArray[2][5];

Here the array declared can be assumed as Row x Column Matrix, If we assume the above array having values as x, y. It can be thought to be like this in the memory:

    01	02	03	04	05

    11	12 	13 	14 	15    

To do get something like above in the memory, one must have written a code like below:

#include <stdio.h>

int  main()
	// declaration of an array having 2 rows and 5 columns.	
	char SecondArray[2][5]; 
	char Rowindex,ColumnIndex;

	//Below we assign value to the 1st row and repective columns
	SecondArray[0][0]= 01 ;  
	SecondArray[0][1]= 02 ;
	SecondArray[0][2]= 03 ;
	SecondArray[0][3]= 04 ;
	SecondArray[0][4]= 05 ;

	//Below we assign value to the 2nd row and repective columns
	SecondArray[1][0]= 11 ;
	SecondArray[1][1]= 12 ;
	SecondArray[1][2]= 13 ;
	SecondArray[1][3]= 14 ;
	SecondArray[1][4]= 15 ;

	//print this 
	return 0;


	1	2	3	4	5
	11	12	13	14	15

So the 2 dimensional arrays when used can store different set of data, and it is understood that the 2nd dimensional arrays can store more values than the 1 dimensional one.

Related Tutorials

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.