Array in C programming – Programmer's view
81What 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:
- One dimension array (Also known as 1-D array).
- Two dimension array (Also known as 2-D array).
- Multi-dimension array.
Best at Amazon
|
Microsoft Windows 7 Professional Upgrade
Price: Too low to display
List Price: $199.99 |
|
Microsoft Windows 7 Ultimate
Price: Too low to display
List Price: $319.99 |
|
|
Microsoft Office 2008 for Mac Home & Student Edition
Price: $84.95
List Price: $149.95 |
|
|
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:
Bestseller Music Downloads
|
Thriller (25th Anniversary Edition)
Price: $11.99
|
|
The E.N.D. (The Energy Never Dies)
Price: $5.00
|
|
Bad
Price: $11.99
|
|
Dangerous
Price:
|
|
Perfecto Vegas
Price:
|
|
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.
Other C Programming tutorials
- How to work with Two Dimensional 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 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... - Types of Function in C Programming Languages:
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: ... - File Copy Program in C Language
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... - Turn Your PC Keyboard to Musical Keyboard using C Program
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...
Your opinion required
This tutorial helped me to improve my C Array understanding.
See results without votingUseful 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... - Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you don’t 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... - 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 ... - A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called...
PrintShare it! — Rate it: up down flag this hub
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










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.