Single Dimensional Array in C Programming Language

Rumman Ansari   Software Engineer   2023-09-01   27930 Share
☰ Table of Contents

Table of Content:


The C language provides a capability that enables the user to design a set of similar data types, called array. This tutorial describes how arrays can be created and manipulated in C.

Why array is important ?

As you might already now, data types can store only one value at a time. Therefore, you would not be able to have a data type that contains more than one slot to store more than one variable. This is where arrays come in. Arrays are simply data types that can store more than one variable. Each variable is stored in an array element.


What are Arrays ?

An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays can have from one to several dimensions. The most common array is the string, which is simply an array of characters terminated by a null.


Types of Arrays in C

Types of Array in c programming language

Single-Dimension Arrays

Now we will discuss about Single-Dimension Arrays in c programming language.


Array Declaration

Like other variables, arrays must be explicitly declared so that the compiler can allocate space for them in memory.

The general form for declaring a single-dimension array is

datatype var_name[size];

Here, type declares the base type of the array, which is the type of each element in the array

size defines how many elements the array will hold.

int rollNo[10]; int marks[10] ; Types of Array in c programming language

Array Initialization

We managed to store values in them during program execution. Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.

int roll[6] = { 2, 4, 12, 5, 45, 5 } ;
int array[ ] = { 2, 4, 12, 5, 45, 5 } ;
float points[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
Note the following points carefully:

(a) Till the array elements are not given any specific values, they are supposed to contain garbage values.
(b) If the array is initialized where it is declared, mentioning the dimension of the array is optional as in the 2nd example above


Accessing Elements of an Array

Once an array is declared, let us see how individual elements in the array can be referred. This is done with a subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus, roll[2] is not the second element of the array, but the third.


marks[0] = 1st Array Element
marks[1] = 2nd Array Element
marks[2] = 3rd Array Element
marks[3] = 4th Array Element
marks[4] = 5th Array Element
marks[5] = 6th Array Element
marks[6] = 7th Array Element
marks[7] = 8th Array Element
marks[8] = 9th Array Element
marks[9] = 10th Array Element

Entering/Inserting Data into an Array

You can places data into an array by specifing the index positions:

marks[0] = 12 ;
marks[1] = 13 ;
marks[2] = 14 ;
marks[3] = 15 ;
marks[4] = 16 ;
marks[5] = 17 ;
marks[6] = 18 ;
marks[7] = 19 ;
marks[8] = 10 ;
marks[9] = 11 ;

Here is the section of code that places data into an array:

 
for ( i = 0 ; i <= 10 ; i++ )
{
printf ( " Enter marks: " ) ;
scanf ( "%d", &marks[i] ) ;
}


Reading/Accessing Data from an Array

Access specific element using their index value

 
 printf("%d", marks[0] ) ;
 printf("%d", marks[1] ) ;
 printf("%d", marks[2] ) ;
 printf("%d", marks[3] ) ;
 printf("%d", marks[4] ) ;
 printf("%d", marks[5] ) ;
 printf("%d", marks[6] ) ;
 printf("%d", marks[7] ) ;
 printf("%d", marks[8] ) ;
 printf("%d", marks[9] ) ;  

Access All element using for loop or any other loop


for ( i = 0 ; i <= 10 ; i++ )
{
 printf("%d", marks[i] ) ;
}

Example Program

Program 1

  
#include<stdio.h>
void main( )
{
 
int marks[10] ; /* array declaration */

 marks[0] = 11; /* store data in array */
 marks[1] = 12;
 marks[2] = 13;
 marks[3] = 14;
 marks[4] = 15;
 marks[5] = 16;
 marks[6] = 17;
 marks[7] = 18;
 marks[8] = 10;
 marks[9] = 11;
 	 
 printf("\nEnter marks %d",marks[0]) ; /* read data from an array*/
 printf("\nEnter marks %d",marks[1]) ;
 printf("\nEnter marks %d",marks[2]) ;
 printf("\nEnter marks %d",marks[3]) ;
 printf("\nEnter marks %d",marks[4]) ;
 printf("\nEnter marks %d",marks[5]) ;
 printf("\nEnter marks %d",marks[6]) ;
 printf("\nEnter marks %d",marks[7]) ;
 printf("\nEnter marks %d",marks[8]) ;
 printf("\nEnter marks %d",marks[9]) ;
  
}
 

Output


Enter marks 11
Enter marks 12
Enter marks 13
Enter marks 14
Enter marks 15
Enter marks 16
Enter marks 17
Enter marks 18
Enter marks 10
Enter marks 11 

Program 2

  
#include<stdio.h>
void main( )
{ 
int i ;
int marks[10] ; /* array declaration */

	for(i = 0 ; i < 10 ; i++){
	printf ( "\nEnter marks:") ;
	scanf ( "%d", &marks[i] ) ; /* store data in array */
	}
	
 for(i = 0 ; i < 10 ; i++){ 
	printf("\nEnter marks %d",marks[i]) ; /* read data from an array*/
   }
}
 

Output

  
Enter marks:12

Enter marks:13

Enter marks:14

Enter marks:15

Enter marks:16

Enter marks:17

Enter marks:18

Enter marks:19

Enter marks:10

Enter marks:11

Enter marks 12
Enter marks 13
Enter marks 14
Enter marks 15
Enter marks 16
Enter marks 17
Enter marks 18
Enter marks 19
Enter marks 10
Enter marks 11 

What is integer array in C programming?

An integer array consists of only integer numbers, for instance, if you have the array of size 5 with interger type data int_array[5] it means that your first element int_array[0] is an integer number like 1, or 15 and so on. The same is true for other elements too; int_array[1] (int_array[2], int_array[3],int_array[4]) might be any integer element and so on.

Another Example

 
#include<stdio.h>
int main(void)
{
int n[100]; /* this declares a 100-integer array */
int t;

/* load x with values 0 through 99 */
for(t=0; t<100; ++t)
 {
  n[t] = t;
 }
 
    /* display contents of x */
for(t=0; t<100; ++t)
 {
   printf("%d \t", n[t]);
 }
    return 0;
}

 

Output

0       1       2       3       4       5       6       7       8       9
10      11      12      13      14      15      16      17      18      19
20      21      22      23      24      25      26      27      28      29
30      31      32      33      34      35      36      37      38      39
40      41      42      43      44      45      46      47      48      49
50      51      52      53      54      55      56      57      58      59
60      61      62      63      64      65      66      67      68      69
70      71      72      73      74      75      76      77      78      79
80      81      82      83      84      85      86      87      88      89
90      91      92      93      94      95      96      97      98      99
Press any key to continue . . .

Another Example on Average

 
#include<stdio.h>
void main()
{
int average, add = 0 ;
int i ;
int marks[10] ; /* array declaration */
for ( i = 0 ; i < 10 ; i++)
 {
	printf("\nEnter marks ") ;
	scanf("%d",&marks[i]) ; /* store data in array */
 }
 
for ( i = 0 ; i < 10 ; i++ ){
	add = add + marks[i] ; /* read data from an array*/
	average = add / 10 ;
  }
printf ( "\nAverage marks = %d \n", average ) ;
}

 

Output


Enter marks 1

Enter marks 2

Enter marks 3

Enter marks 4

Enter marks 5

Enter marks 6

Enter marks 7

Enter marks 8

Enter marks 9

Enter marks 10

Average marks = 5
Press any key to continue . . .

What is the application of arrays in C programming?

Array is used in C to store data of similar data type. Arrays are used when we want to store data in large quantities, e.g. if we want to store 100 numbers we have to declare 100 variables and remembering the name of each variable is a very a difficult task, here comes the array we can declare a single variable int num[100] now this variable can store 100 different or same numbers and can be accessed by referencing as num[0], num[1], num[2] and so on. So, we can say that to reduce efforts we use arrays.


What is Array in c programming?

An array in C programming is a homogeneous user-defined datatype consisting of multiple data elements occupying contiguous memory locations. Homogeneous means that the array can only consist of elements of a single data type. Contiguous means that the elements of the array occupy (logically) adjacent memory locations.


What is the importance of array in C programming?

Arrays allow similar types of data to be stored within a contiguous block of memory such that every data element is accessible in constant time, regardless of its physical location within the array. This is achieved through simple pointer arithmetic treating each element as a memory offset from the start of the array. Since every element is the same length (in bytes), locating any element is simply a matter of calculating its offset from its index. Indices are zero-based thus the third element can be found at index 2. The memory offset for that element is therefore the product of the element size and 2. However, C permits indices to be specified directly, while the pointer arithmetic is done in the background. Thus array_name[2] automatically returns a reference to the third element. Arrays with large and complex variable length data elements need to store those elements separately from the array, usually non-contiguously. This is achieved by using a pointer array. Pointer arrays are particularly useful when sorting extremely large data lists as it is much easier and more efficient to implement a sorting algorithm with an array than it is with a linked list, particularly when constant-time random-access is essential to the algorithm. The time and effort in building the array is generally more than compensated for by the efficiency of the algorithm. Arrays can also be divided and subdivided to better model the data they represent. For instance, a chessboard might be implemented as a one-dimensional array of 64 elements, however it makes more sense to model the chessboard in a two-dimensional array of 8x8 elements. Although the array is still allocated contiguously and can be thought of as being 8 rows and 8 columns, it's actually better to think of this two-dimensional array as being a one-dimensional array of 8 elements, where each element is another one-dimensional array of 8 elements. By thinking this way it makes it possible to allocate extremely large arrays in non-contiguous memory (as completely separate one-dimensional arrays) and also makes comprehension of a four-dimensional array in a three-dimensional world that much easier (unless you actually want to model time and space of course).A four-dimensional array can be thought of in a variety of ways: as being a one dimensional array of three-dimensional arrays, or as a two-dimensional array of two-dimensional arrays, or as a three-dimensional array of one-dimensional arrays, or even as a one-dimensional array of one-dimensional arrays of one-dimensional arrays of one-dimensional arrays. Whichever method you use to imagine your array is immaterial, so long as it makes sense to you that's all that really matters.