Flag This Hub

C Programming Lesson - Arrays

By


What is an Array in C?

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). Discussed below in this tutorial.
  2. Two dimension array (Also known as 2-D array). Click on link to see the tutorial
  3. Multi-dimension array. Click on link to see the tutorial

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:

C language books on Amazon

C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
C (Vintage)
Amazon Price: $1.03
List Price: $15.00
Programming in C (3rd Edition)
Amazon Price: $24.00
List Price: $49.99
C Primer Plus (5th Edition)
Amazon Price: $30.63
List Price: $54.99
Absolute Beginner's Guide to C (2nd Edition)
Amazon Price: $19.00
List Price: $39.99

One Dimensional Array

This image shows how 1D array is stored in memory. You can access any value of arrays based on index.
This image shows how 1D array is stored in memory. You can access any value of arrays based on index.

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();
}
This out shows you how to do array initialization and display values stored in array in C.
This out shows you how to do array initialization and display values stored in array in C.

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

Josh 2 years 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 2 years ago

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

R.Dhnanalakshmi 2 years ago

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

Thank Sir

nataraju 2 years 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

alia 2 years ago

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

anne khandelwal 21 months ago

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

jhesmer 21 months ago

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!!

zeke 19 months ago

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...

LeAnn 19 months ago

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! :)

an 17 months ago

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

madbones 16 months ago

harigatou gozaimasu!

prakash bannadabhavi 16 months ago

#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

kaushik 15 months ago

i need more tutorial session suggest us more

vinodh 15 months ago

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

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

christiyal jansi rani 15 months ago

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

miles 14 months ago

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

miles 14 months ago

Email me the answer please...

fabulousmiles_13@yahoo.com...

enzoh_22 14 months ago

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

prashant kedar 9 months ago

i like

bulbul 9 months ago

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

ritesh 8 months ago

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

Bikram anand ,barari 8 months ago

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

anne 8 months ago

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

mamatha.k 8 months ago

plese try to explian with some more examples

julie 8 months ago

good

michelle 8 months ago

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

nikhil 8 months ago

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

snkhan120 8 months ago

array is a heart of programming

vishal gurhale 7 months ago

i want detail in array

like as

how initialize of array.

and how to work array in program

deepak vaishnav 7 months ago

nice programming and any another program,

kavitha 7 months ago

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

pooja 7 months ago

i like thankyou so much

elkan 7 months ago

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

shonah 7 months ago

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

jyoti 7 months ago

plz give me some program in character a array

kalaiselvi 7 months ago

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

Jaiyaishri 7 months ago

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

Keya Mondal 7 months ago

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

please help me about stack

abhi 7 months ago

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

lee 7 months ago

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

ahmad gill 6 months ago

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

ziaah 6 months ago

Write a c++ program?

a.declare an integer array of 5?

lavanya 6 months ago

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

janu 6 months ago

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

alvina 6 months ago

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

lavanya 6 months ago

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.

Ana 6 months ago

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

rajib 6 months ago

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

lemiey 5 months ago

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.

Saroar Hossain 5 months ago

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

momai 5 months ago

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

ash 5 months ago

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

pooja.i 5 months ago

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

ishaq 4 months ago

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

Shalini 4 months ago

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

sonali ananthre 4 months ago

please help me to sort

sruthi 4 months ago

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

babegirl15 4 months ago

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

sam 4 months ago

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

Emmanuel 3 months ago

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

don 3 months ago

how to reverse a printout of an array?

mkeith 3 months ago

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.

Java Programs 2 months ago

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

indu 2 months ago

plzz tell me how to proiduce nested loop??

ridhima 2 months ago

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

hhh...d 2 months ago

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

سووووووووسو 8 weeks ago

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?

ronnie 6 weeks ago

please help me solve this question...

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

suraweera 6 weeks ago

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

ricardo 5 weeks ago

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

saiba 4 weeks ago

hi

write one more programme for multi dimentional array..

mikzz 2 weeks ago

int array [9][9];

assigning values from 1 to 9

magic square..

Submit a Comment
Members and Guests

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



    Like this Hub?
    Please wait working