Updated date:

What Is an Array in C Programming Language

Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.

an array in c programming language with example

an array in c programming language with example

What is an Array in C?

In the programming language C, an array is a collection of values of a given data type. Arrays can be created from any of the C data types, such as "int," "float," and "char." Arrays can only hold data of their designated type: for example, an integer array can only hold integer values.

How does an array differ from a variable? When we declare array, it allocates a contiguous memory location for storing values, whereas two or three variables of same data-type can be given random locations. Arrays store data under a single variable name and index.

There are different types of arrays:

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

This tutorial focuses on one-dimensional array.

Declaration of One Dimensional Arrays:

To declare a one-dimensional array in C, use the following syntax: data_type array_name[width];

Example: Declare an array that can store the roll numbers of eight students in a class, for the purpose of taking roll.

Answer: int roll[8];

In our example:

  • int specifies the type if the variable. In this example, an integer;
  • roll specifies the given name of the variable;
  • [8] is the value;
  • The brackets ([ ]) tell the compiler that what follows is an array, and the number in the brackets specifies how many elements (values in any array are called elements) it can store. This number is also the size of array.

In this example we have declared an integer array named“roll” that can store the roll numbers of eight students.

You can see the memory arrangement of the above declared array in the following image:

this is how array is stored in memory and value stored in it can be accessed by index.

this is how array is stored in memory and value stored in it can be accessed by index.

C Array Assignment and Initialization

We can assign values to arrays just as we assign them to variables. We can initialize an array (assign its initial values) just was we initialize a variable. Value can be assigned to an array at the time of declaration or during runtime. Let’s look at each approach.

The syntax for assigning value to an array is: data_type array_name[size]={list of values};

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

Both these examples are valid ways of declaring an integer array named “arr,” stating that it can hold five elements, and initializing it by listing the values it holds. The first example mentions the size of the array—[5]—and assigns the values to the elements by putting the values in curly brackets, separating each element’s value with a comma.

In the second example we left the “size” field blank, but we provided values for five elements. When we only give element values without specifying the size of an array, then the C compiler automatically assumes an array size based on the number of element values given (in this case, five).

Initializing an Array by Assigning Individual Variables

There is one more way to initialize an array C programming. In this method we can assign values to each individual element of an array. See the example below.

Syntax:

#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();
}
output of array in c program

output of array in c program

In this example, we have assigned individual values to the integer array, just as we would to an integer variable. We called the value of each array element individually, and used a for loop operation so it would be clear for beginner C programmers.

This example shows we can assign individual values to array elements, and call the values whenever we need them.

Reader's Feedback

© 2009 RAJKISHOR SAHU

Comments

umeed on October 03, 2014:

Plzzz help me!

How to write a progam that shows 3 no. of rows ,two having lists of integers with size [9] and the third shows their sum by the help of array?????????

improzee on September 29, 2014:

please give me a sample of this program.

A software company has requested you to implement a Computer Information System that will keep up to a maximum of 50 Computer data. The computer’s information to be kept is:

• ComputerID – int

• ComputerName – 50 characters

• ProcessorSpeed – integer

• RAM - Integer

• OperatingSystem – 50 characters

• Price - float

You would need to create a Computer structure and an array called Computers. You are requested to test the Computer Information System developed by performing the tasks b) to e), and display its output.

a) Declare the Computer structure and the Computers array, using the information given above.

b) Insert 5 new computers using a sequential ComputerID

c) Search for a particular computer by ComputerID

d) Search for a particular computer by ComputerName

e) Display a list of all computers

cokemaster on September 03, 2014:

great hub...

manisha on January 01, 2014:

i want user se 10 no. enter kraye then user hi unme se koi no. search kare ki wo no. jo user ne enter kiye unme h ya nhi agar h toh print ho "element found" agar nhi toh print ho "element no found"

annabelle_carza@yahoo.com on August 31, 2013:

hi! hello! mam ,sir, ahm i need some example in arrays in how to run a programm..

deepanshu on February 01, 2013:

good information

mikzz on May 09, 2012:

int array [9][9];

assigning values from 1 to 9

magic square..

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on April 25, 2012:

2d array :https://hubpages.com/technology/How-to-work-with-T...

multi D array : https://owlcation.com/stem/How-to-work-with-Multid...

saiba on April 24, 2012:

hi

write one more programme for multi dimentional array..

ricardo on April 17, 2012:

hi...i had to put it this way to get the results:

main (void)

and

remove the clrscr();

to get it worked in Dev c++...

nevertheless...it was of great help :D

thanks

suraweera on April 11, 2012:

please, help me to write C program to display index number & name of 5 students by using array based list.

ronnie on April 11, 2012:

please help me solve this question...

using arrays write a simple program to display numbers 1-20.

سووووووووسو on March 28, 2012:

consider the following java statement double [] A1 = new double [7]; int A2 [],A3; A2 ={2,4,10);

1) how many arrays in the previous code?name them

2)what is the data type of A3?

3)draw the memory address for A1,A2,A3.

4)what is the output of the follwing code?

hhh...d on March 13, 2012:

write a program read the integer 10 element into an array and display it in reverse order?

ridhima on March 10, 2012:

plzz tell me a c++ program to multiply a number x upto n terms...????

indu on March 10, 2012:

plzz tell me how to proiduce nested loop??

Java Programs from India on March 10, 2012:

one of the best article i have seen for c programming dealing with arrays ..... keep the good work up ..

mkeith from United States on February 24, 2012:

Your "Memory Arrangement" in your diagram is likely wrong. Computers have been running a 32-bit os since the mid to late 90's. If you are programming on a windows machine then an 'int' variable type gets 4 bytes of data, not 2. If you are programming for some other type of processor that only has a 16 bit integer (word size) then this is fine, but you need to declare this as it might confuse others.

don on February 07, 2012:

how to reverse a printout of an array?

Emmanuel on February 05, 2012:

Hello,

I need someone to help me solve this questions (C programming).

You have a two-dimensional array [ 4 ] [5 ].

a) make a program that calculates the sum of the elements.

b) make a program that finds the entered value from the array and prints out ”success” if found and ”empty” if not found.

c) change the exercise b so that it prints out the place from where the program found the entered value .

Example of initializing a two-dimensional array:

int array [ 4 ] [5 ] =

{ { -2, 5, -7, 2, 8},

{ 80, 32, 4, 5, 6},

{ -1, 0, 4, 82, 6},

{ 3, -5, 4, 7, 8}

};

Send the solution to my email address: emma4ever01@yahoo.ca

sam on January 24, 2012:

how to print an element a[3] of array a[10] if values has been entered

babegirl15 on January 21, 2012:

can you help me with this?

Assigning values to array through input operation.

like this.

content of array

locaion value

o 1000

1 0

2 0

3 0

4 0

Datay entry using array

Enter location: 0

Enter value:

Accept another value?

please email me the answer

ruthalfonso32@yahoo.com

sruthi on January 17, 2012:

write a programme for multi dimentional array...........

sonali ananthre on January 12, 2012:

please help me to sort

Shalini on January 11, 2012:

Write the code for the below, the input which we give looks like this.

enter the number of element

2

enter the number of materials

1

enter the material details

1

E1,E2,E3,G12,G13,G23,v12,v13,v23,rho

details of material 1

thickness angle material

details of material 2

thickness angle material

ishaq on January 11, 2012:

please make a program which show yout id is vaild or not

pooja.i on December 27, 2011:

write a c program to entering a 3 nos and print 3 nos using array

ash on December 21, 2011:

how to write up a program that will store five random integer numbers in an array and find the highest and lowest number stored and display the highest and lowest value in c++ programming

momai on December 17, 2011:

...kindly help me to do programming about phonebook using the array program.....

Saroar Hossain on December 01, 2011:

Searching one dimensional array:

Write a program that takes 10 values from the user, stores it in an array and allows the user to look for a value in that array (after the array is created, ask the user for the value they want to search for in the array, write a searching algorithm which looks for that value in the array and if found tells the user about its location [index no.] if not found informs the user that no matching results were found)

Matrix multiplication using

I am really very confused. Can you please help me solving this problem? Please

lemiey on December 01, 2011:

my question is : how to write a program that can read 10 integer values into an array and find the total for that array in the program,write 3 function:a function to read value, a function to find the total of element values and a function to print value.

rajib on November 28, 2011:

plz write a program to find 1st and 2nd largest no using array

Ana on November 26, 2011:

please help me

write a complete program that will ask 10 integer numbers using an array. the program will display the values in revers order.

thanks

lavanya on November 26, 2011:

pls any one send this program answer ,Twenty five numbers are entered through the keyboard into an arry.Write a program to find out how many of them are positive,how many are negative,how many are even and how many are odd.

alvina on November 23, 2011:

can any one help me now? i need a program which can read 25 integer values n print it in ascndng ordr

janu on November 11, 2011:

how a print a character in a array "multiple character"

lavanya on November 07, 2011:

write a program to get marks of 5 different subjects for 10 students and print the same.

ziaah on November 04, 2011:

Write a c++ program?

a.declare an integer array of 5?

ahmad gill on November 01, 2011:

who can find solution???????

#include

#include

void mtrx_sort(int ary[][3]); //function prototype

main()

{ clrscr();

int x,y=2;

int ary[3][3];

cout

lee on October 25, 2011:

please help me how to fill matrix arr[i][j] with elements which i could later round to nearest integer.!!

abhi on October 18, 2011:

i m facing problem to WAP in c language using Array.2d,multi d.plz give me good idea to understood

Keya Mondal on October 18, 2011:

hi, my email id softprog.program5@gmail.com

please help me about stack

Jaiyaishri on October 14, 2011:

good example.it's eassy to lurn & understand.

kalaiselvi on October 12, 2011:

i want this program multipulation of array, pls send my mail id my mail id is kalaithangaraj1@gmail.com

jyoti on October 10, 2011:

plz give me some program in character an array

shonah on October 10, 2011:

pls help a sister out

Write a C program that displays an array of 10 different double numbers. The numbers should be values that you assign to the array by hard-coding them in the main().

The numbers should be displayed using a function that you write called DisplayNumbers.

Write another function called ReverseArrayNumbers that:

creates a second array of the same size as the first one

copies the numbers from the first array into the new array in reverse order.

Display the numbers in the new array by using the DisplayNumbers ...

email it to: shonahj@gmail.com

Thank you

elkan on October 08, 2011:

Pls help me

Write a C program that displays an array of 10 different double numbers. The numbers should be values that you assign to the array by hard-coding them in the main().

The numbers should be displayed using a function that you write called DisplayNumbers.

Write another function called ReverseArrayNumbers that:

creates a second array of the same size as the first one

copies the numbers from the first array into the new array in reverse order.

Display the numbers in the new array by using the DisplayNumbers ...

email it to: etandreas@gmail.com

Thank you

pooja on October 07, 2011:

i like thankyou so much

kavitha on October 06, 2011:

i want answer this question please.

write a program to copy the contents of one array into another array in the reverse order.please answer to the question

deepak vaishnav on October 03, 2011:

nice programming and any another program,

vishal gurhale on October 01, 2011:

i want detail in array

like as

how initialize of array.

and how to work array in program

snkhan120 on September 29, 2011:

array is a heart of programming

nikhil on September 18, 2011:

how to make a programe to find out the greatest element in an array in c languae !!

michelle on September 09, 2011:

Ma'am/Sir i need your help. please help me regarding my assignment, I want to know more about programming.here's the formula:

char a[11]="Boring";

printf("Index 0 has %c\n",a[0];

printf("Index 1 has %c\n",a[1];

printf("index 2 has %c\n",a[3];

printf("Index 3 has %c\n",a[4];

printf("Index 4 has %c\n",a[5];

printf("Index 5 has %c\n",a[6];

printf("Index 6 has %c\n",a[7];

printf("Numerically,the a[6] is %d\n",a[6]);

Question:

a. How many slots/indexes does this array have?

b. How many characters, counting the null character at the end,does this array hold?

c. The number of an index is also called an index. What is the lowest index?

d. What is the highest index for this array?

e. Is the number for the highest index the same as that for the number of indexes?

f. What is stored in the last index as a character,that is, in an index number 5?

g. What is stored in the last index numerically? All strings should have this null character stored in its last index to signal the end of the string.

I hope you can help me regarding my problem. This is my email id: michelle_sph2001@yahoo.com

julie on September 07, 2011:

good

mamatha.k on September 07, 2011:

plese try to explian with some more examples

anne on September 07, 2011:

thank u so much,,its such a great help!!

Bikram anand ,barari on September 06, 2011:

Sir, this tutorial is nice but i have requested to should add some more eaisy example with output.thanx bikram1023@yahoo.com

ritesh on September 04, 2011:

in 1-D array i want access the all array elements to the other functions too other functions and i want to doing actually quick sort so i have change index of array all times. im trying so much but not getng output. so plz tell me as soon as fast possible. i need it.

my emailid : riteshpatel1989@gmail.com

bulbul on August 29, 2011:

sir\madam actually i need ur help to do the programing of multidimensional array.The programme is to evaluate each student total value and final total value.

plz help me how can i do this program by using which logic

My emailid is: mail2blogspot@gmail.com

prashant kedar on August 24, 2011:

i like

enzoh_22 on March 22, 2011:

write a program containing a function that will compute and display the answer of various statistical measurement like a. combination and permutation b. mean, median, mode.

e-mail to lorenz_florendo@yahoo.com thnks

miles on March 09, 2011:

Email me the answer please...

fabulousmiles_13@yahoo.com...

miles on March 09, 2011:

hi can u create a program that will read 10 characters(char) and store in an array.And display the count of each character being stored (please see the sample illustration below).

Sample Illustration:

char A[5];

Assume:

A[0]='y'

A[1]='o'

A[2]='l'

A[3]='l'

A[4]='y'

The counter:

y=2

o=1

l=2

christiyal jansi rani on February 17, 2011:

Respected sir/Madam! I am christi! plz one help doupt for you sir! array initialazation reading writing program ! Its very urgent! plz Condect My ID Name was anbuchristi@ymail.com

vinodh on February 10, 2011:

int a[]={0,1,2,3,4}

i want to display all this in increment way.. tell me the short and simple way

kaushik on February 03, 2011:

i need more tutorial session suggest us more

prakash bannadabhavi on February 01, 2011:

#include

using namespace std;

int main()

{

int x;

int y;

int array[8][8]; // Declares an array like a chessboard

for ( x = 0; x < 8; x++ ) {

for ( y = 0; y < 8; y++ )

array[x][y] = x * y; // Set each element to a value

}

cout

madbones on January 17, 2011:

harigatou gozaimasu!

an on December 09, 2010:

these has ben a good help to me! thanks to you! im having a hard time understanding all the cides and terms in our programming subject! our prof gave us an assign. in which we will create a program ussing array!!!!!can u help me? i need it asap thankssssss

LeAnn on October 20, 2010:

I really need some help!

i have to:

1. create an array of 7 integers

2. ask user to give the values of its elements and fill the array

3. find the minimum of values in the array and print it

please help!

email it to me @ leann_marie06@yahoo.com

it is due in 6 hours. I am trying my best to do this, but i'm having a lot of difficulties.

THANKS SO MUCH! :)

zeke on October 05, 2010:

could u create a program which will input 15 car names

and will get the choice,quantity,payment and change of the costumer..please e-mail it to me: ausa_jay@yahoo.com

thank you & god bless...

jhesmer on August 30, 2010:

plzz create a program that will input 10 numbers and will output the 10 numbers from least to greatest

example input: example output:

9 0

0 1

8 2

1 3

7 4

2 5

6 6

3 7

5 8

4 9

plzz do e-mail it to me: jherobe_zae@yahoo.com

a big thankzz to everyone!!

anne khandelwal on August 20, 2010:

how to write a program to generate prime numbers from 1to 100??with explanation..plz help me..

alia on December 19, 2009:

how to write programs using array???can u give some examples

nataraju on October 26, 2009:

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

R.Dhnanalakshmi on September 18, 2009:

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

Thank Sir

nicomp on September 10, 2009:

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

Josh on August 25, 2009:

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.