C Programming Lesson - Arrays
By rajkishor09
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:
- One dimension array (Also known as 1-D array). Discussed below in this tutorial.
- Two dimension array (Also known as 2-D array). Click on link to see the tutorial
- 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 eBay
|
|
ABEKA 6th GRADE LANGUAGE C SET OF 4 BOOKS SAVE $$
Current Bid: $46.21
|
|
|
Sonlight Language Arts 4 & 5 Instructor's Guide c2011 with 16 books
Current Bid: $125.00
|
|
|
C Programming Language by Dennis M. Ritchie and Brian W. Kernighan
Current Bid: $14.95
|
|
|
The C Programming Language by Brian W. Kernighan (1988, Paperback)
Current Bid: $30.90
|
| No Photo |
Pro LINQ: Language Integrated Query in C# 2010 - Paperback
Current Bid: $16.19
|
|
|
ESSENTIALS OF LATIN BY JOHN F C RICHARDS LANGUAGE STUDY BOOK COLUMBIA UNIVERSITY
Current Bid: $16.96
|
C language books on Amazon
![]() | Amazon Price: $35.64 List Price: $67.00 |
Amazon Price: $1.03 List Price: $15.00 | |
![]() | Amazon Price: $24.00 List Price: $49.99 |
![]() | Amazon Price: $30.63 List Price: $54.99 |
![]() | Amazon Price: $19.00 List Price: $39.99 |
One Dimensional Array
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.
See results without votingC programming tutorial
- 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 - 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. - C Programming Lesson - Call by Value and Call by Reference
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 - C Programming - Basic Virtual Keyboard Application
To accomplish this task you dont 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... - C Programming Lesson - Do-While Loop in C Programming 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 Lesson - For loop in c programming language and how for loop works
Along with while loop and do-while loop, for loop is also part of C programming language. For many programmers, for loop seems to be easiest loop comparing other two loops (while and do-while loop). Mainly its structure (syntactically) makes it easy - C Programming Lesson - While loop in C Programming
I already explained what is loop and why we use loop in c language, and I am not going to repeat that here again. So we will start with while loop explanation. While loop is one of the simple loop available in C language.It’s very easy to use, lets c - C Programming Lesson - Looping in C Programming, Types of Loop and Loop Example
Loop is one of the important parts of C language and study of C language is incomplete without this. So let’s head towards completion of our knowledge about C language. As a dedicated C language leaner, right now you should have two questions. What i - How to swap two numbers with and without using third (temp) variable in C , C# and Java
This article explains how we can swap variable values without using third variable. There are different ways to do so and I tried including all know methods. You can get code in C, Java and C# language. - 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 ... - C Programming Lesson - Types of Function
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are: ... - How to work with Two Dimensional (2d) Arrays in C
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... - How to work with Multidimensional (3D) Array in C Programming
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... - C Programming Lesson - File Copy Program in C
Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok lets start. The main...
Useful Articles
- How to setup network between XP and Vista PCs
Vista is Microsoft’s 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... - 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... - How to enable task manager and registry editor (regedit) with few clicks
If you are a computer user then you might also have heard about viruses. There are few viruses which do havoc on prey computer and changes its behavior drastically. I also use computer and many time got some... - 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 ...
Comments
An array dimensioned as [5] only has elements 0 through 4. It has no element [5]. Your code is incorrect.
I Have some information in 'c'program,but more detail in array concept please sir.
Thank Sir
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
how to write programs using array???can u give some examples
how to write a program to generate prime numbers from 1to 100??with explanation..plz help me..
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!!
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...
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! :)
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
harigatou gozaimasu!
#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
i need more tutorial session suggest us more
int a[]={0,1,2,3,4}
i want to display all this in increment way.. tell me the short and simple way
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
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
Email me the answer please...
fabulousmiles_13@yahoo.com...
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
i like
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
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
Sir, this tutorial is nice but i have requested to should add some more eaisy example with output.thanx bikram1023@yahoo.com
thank u so much,,its such a great help!!
plese try to explian with some more examples
good
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
how to make a programe to find out the greatest element in an array in c languae !!
array is a heart of programming
i want detail in array
like as
how initialize of array.
and how to work array in program
nice programming and any another program,
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
i like thankyou so much
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
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
plz give me some program in character a array
i want this program multipulation of array, pls send my mail id my mail id is kalaithangaraj1@gmail.com
good example.it's eassy to lurn & understand.
hi, my email id softprog.program5@gmail.com
please help me about stack
i m facing problem to WAP in c language using Array.2d,multi d.plz give me good idea to understood
please help me how to fill matrix arr[i][j] with elements which i could later round to nearest integer.!!
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
Write a c++ program?
a.declare an integer array of 5?
write a program to get marks of 5 different subjects for 10 students and print the same.
how a print a character in a array "multiple character"
can any one help me now? i need a program which can read 25 integer values n print it in ascndng ordr
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.
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
plz write a program to find 1st and 2nd largest no using array
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.
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
...kindly help me to do programming about phonebook using the array program.....
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
write a c program to entering a 3 nos and print 3 nos using array
please make a program which show yout id is vaild or not
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
please help me to sort
write a programme for multi dimentional array...........
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
how to print an element a[3] of array a[10] if values has been entered
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
how to reverse a printout of an array?
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.
one of the best article i have seen for c programming dealing with arrays ..... keep the good work up ..
plzz tell me how to proiduce nested loop??
plzz tell me a c++ program to multiply a number x upto n terms...????
write a program read the integer 10 element into an array and display it in reverse order?
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?
please help me solve this question...
using arrays write a simple program to display numbers 1-20.
please, help me to write C program to display index number & name of 5 students by using array based list.
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
hi
write one more programme for multi dimentional array..
2d array :http://rajkishor09.hubpages.com/hub/How-to-work-wi
multi D array : http://rajkishor09.hubpages.com/hub/How-to-work-wi
int array [9][9];
assigning values from 1 to 9
magic square..




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.