create your own

Array in C programming – Programmer's view

81
rate or flag this page

By rajkishor09


What is an Array?

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. So an integer array can only hold integer values and cannot hold values other than integer. When we declare array, it allocates contiguous memory location for storing values whereas 2 or 3 variables of same data-type can have random locations. So this is the most important difference between a variable and an array.

Types of Arrays:

  1. One dimension array (Also known as 1-D array).
  2. Two dimension array (Also known as 2-D array).
  3. Multi-dimension array.


C Language Tutorial
C Language Tutorial

Best at Amazon

Microsoft Windows 7 Professional Upgrade Microsoft Windows 7 Professional Upgrade
Price: Too low to display
List Price: $199.99
Microsoft Windows 7 Ultimate Microsoft Windows 7 Ultimate
Price: Too low to display
List Price: $319.99
Microsoft Office 2008 for Mac Home & Student Edition Microsoft Office 2008 for Mac Home & Student Edition
Price: $84.95
List Price: $149.95
Apple MacBook Pro MB990LL/A 13.3-Inch Laptop Apple MacBook Pro MB990LL/A 13.3-Inch Laptop
Price: Too low to display
List Price: $1,199.00

Declaration of One Dimensional Arrays:

Syntax: data_type array_name[width];

Example: int roll[8];

In our example, int specifies the type if the variable, roll specifies the name of the variable and the value in bracket [8] is new for newbie. The bracket ([ ]) tells compiler that it is an array and number mention in the bracket specifies that how many elements (values in any array is called elements) it can store. This number is called dimension of array.
So, with respect to our example we have declared an array of integer type and named it “roll” which can store roll numbers of 8 students. You can see memory arrangement of above declared array in the following image:

One Dimensional Array Memory Arrangement.
One Dimensional Array Memory Arrangement.

Bestseller Music Downloads

Thriller (25th Anniversary Edition) Thriller (25th Anniversary Edition)
Price: $11.99
The E.N.D. (The Energy Never Dies) The E.N.D. (The Energy Never Dies)
Price: $5.00
Bad Bad
Price: $11.99
Dangerous Dangerous
Price:
Perfecto Vegas Perfecto Vegas
Price:
Essential Michael Jackson Essential Michael Jackson
Price: $11.99

C Array Assignment and Initialization:

We can initialize and assign values to the arrays in the same way as we do with variable. We can assign value to an array at the time of declaration or during runtime. Let’s look at each approach.

Syntax: data_type array_name[size]={list of values};

Example:
int arr[5]={1,2,3,4,5};
int arr[]={1,2,3,4,5};

In our above array example we have declared an integer array and named it “arr” which can hold 5 elements, we are also initializing arrays in the same time.

Both statements in our example are valid method to declare and initialize single dimension array. In our first example we mention the size (5) of an array and assigned it values in curly brace, separating element’s value by comma (,). But in second example we left the size field blank but we provided its element’s value. When we only give element values without providing size of an array then C compiler automatically assumes its size from given element values.

There is one more method to initialize array C programming; in this method we can assign values to individual element of an array. For this let’s look at example:

Array Initialization Example

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[5],i;
clrscr();
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;

printf("Value in array arr[0] : %d\n",arr[0]);
printf("Value in array arr[1] : %d\n",arr[1]);
printf("Value in array arr[2] : %d\n",arr[2]);
printf("Value in array arr[3] : %d\n",arr[3]);
printf("Value in array arr[4] : %d\n",arr[4]);
printf("\n");

for(i=0;i<5;i++)
{
	printf("Value in array arr[%d] : %d\n",i,arr[i]);
}
getch();
}

In the above c arrays example we have assigned the value of integer array individually like we do with an integer variable. We have called array element’s value individually and using for loop so that it would be clear for beginner and semi-beginner C programmers. So, from the above example it is evident that we can assign values to an array element individually and can call them individually whenever we need them.

Your opinion required

This tutorial helped me to improve my C Array understanding.

  • Yes.
  • No,
  • This tutorial need improvements (Please suggest this through comments).
See results without voting

Comments

RSS for comments on this Hub

Josh  says:
3 months ago

You mention only 1-dimensional arrays, which are quite basic and simple to understand.

Rather, I think that most users would be looking for help concerning 2-dimensional and multi-dimensional arrays, as these are the ones that are a lot harder to wrap your mind around.

nicomp  says:
3 months ago

An array dimensioned as [5] only has elements 0 through 4. It has no element [5]. Your code is incorrect.

R.Dhnanalakshmi  says:
2 months ago

I Have some information in 'c'program,but more detail in array concept please sir.

Thank Sir

nataraju  says:
5 weeks ago

char a[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

char a1[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

char a2[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3}

char a3[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3}

char a4[]={1,2,3,4,5,6,7,4,33,44,2,1,54,6,7,87,5,4,3,5,7,6,54,4,4,3};

char a5[]={a,a1,a2,a3,a4};

a5 is correct or not

how to store a to a4 in to another array

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working