Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

One Dimensional Array

An array with one subscript is known as a one-dimensional array.

Syntax

data_type arrayName[size];

data_typeTells the type of data like int, float, double, etc.
arrayName –  Tells the name of an array.
size Tells the size of an array, that is the number of elements array can store.

General arrangement of one-dimensional array elements in memory

C Programming Language One Dimensional Array

Example of one-dimensional array

int roll_no[10];

Here the name of an array is roll_no; data type is int & size is 10.
Since data type of array is int, it can contain an element of integer type only.
The size of an array is 10; therefore it is capable of storing 10 elements only & not more than that.
Each element of the array can be obtained by using the array name with a subscript.
Subscript starts from zero and ends with one less than size.
Therefore the first element of an array is accessed using array name with subscript zero, the second element of an array is accessed using array name with subscript one, the third element of an array is accessed using array name with subscript two, and so on as shown below:

roll_no[0] – 1st   element of the array
roll_no[1] – 2nd   element of the array
roll_no[2] – 3rd   element of the array
roll_no[3] – 4th   element of the array
roll_no[4] – 5th   element of the array
roll_no[5] – 6th   element of the array
roll_no[6] – 7th   element of the array
roll_no[7] – 8th   element of the array
roll_no[8] – 9th   element of the array
roll_no[9] – 10th  element of the array

Declaration of One Dimensional array

An array must be declared at the beginning of a program like other variables.
Here are some examples of 1D array declaration.

 int roll_no[10];
 char alphabets[26];
 float percentage[13];

An array roll_no is capable of storing 10 integers.
An array alphabets is capable of storing 26 characters.
An array percentage is capable of storing 13 float values.

Initialization of 1D array

There are many ways to initialize the array & to print its elements. Let’s look at them one by one.

1. Assigning value to each element of array separately

Example

#include <stdio.h>
int main()
{
    int roll_no[5];
    roll_no[0] = 10;
    roll_no[1] = 20;
    roll_no[2] = 30;
    roll_no[3] = 40;
    roll_no[4] = 50;
	
    printf("\n 1st element of array roll_no = %d",roll_no[0]);
    printf("\n 2nd element of array roll_no = %d",roll_no[1]);
    printf("\n 3rd element of array roll_no = %d",roll_no[2]);
    printf("\n 4th element of array roll_no = %d",roll_no[3]);
    printf("\n 5th element of array roll_no = %d",roll_no[4]);
					
    return 0;
}

Output

1st element of array roll_no = 10
2nd element of array roll_no = 20
3rd element of array roll_no = 30
4th element of array roll_no = 40
5th element of array roll_no = 50

2. Initializing array elements one by one using scanf() function

Example

#include <stdio.h>
int main()
{
    int numbers[5];
    int i;
    printf("\n Enter 5 numbers :: ");
    scanf("%d",&numbers[0]);
    scanf("%d",&numbers[1]);
    scanf("%d",&numbers[2]);
    scanf("%d",&numbers[3]);
    scanf("%d",&numbers[4]);
	
    printf("\n\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
    return 0;
}

Output

Enter 5 numbers :: 100
200
300
400
500

Elements of an array numbers are ::
100
200
300
400
500

3. Using scanf() function in loop

Example

#include <stdio.h>
int main()
{
    int numbers[5];
    int i;
	
    printf("\n Enter 5 numbers :: ");
    for(i=0;i<5;i++)
    {
        scanf("%d",&numbers[i]);
    }
	
    printf("\n\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
    return 0;
}

Output

Enter 5 numbers :: 11
22
33
44
55

Elements of an array numbers are ::
11
22
33
44
55

4. Initializing array explicitly at the time of declaration

Example

#include <stdio.h>
int main()
{
    int numbers[5] = {1,2,3,4,5};
    int i;
	
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
    return 0;
}

Output

Elements of an array numbers are ::
 1 
 2 
 3 
 4 
 5

If we initialize an array at the time of declaration, it is not compulsory to declare the size of an array. If the size is not declared, the number of values assigned to the array is considered as its size.

Example

#include <stdio.h>
int main()
{
    int numbers[] = {10,20,30,40,50,60,70}; // compiler assumes size of array
                                            // equal to 7
    int i;
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<7;i++)
    {
        printf("\n %d",numbers[i]);
    }
    return 0;
}

Output

Elements of an array numbers are ::
10
20
30
40
50
60
70

If we did not initialize a local array at the time of declaration, all its elements are assigned with garbage values (any random values).

Example

#include <stdio.h>
int main()
{
    int numbers[5];
    int i;
	
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
	
    return 0;
}

Output

Elements of an array numbers are ::
3
0
42
0
2005216112

At the time of declaration if the number of values assigned to an array is less than the size of an array, then the remained elements are assigned with zero.

Example

#include <stdio.h>
int main()
{
    int numbers[5] = {10,20};
    int i;
	
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
	
    return 0;
}

Output

Elements of an array numbers are ::
 10
 20
 0
 0
 0

At the time of declaration, if the number of values assigned to an array is more than the size of an array, we get the compile time error.

Example

#include <stdio.h>
int main()
{
    int numbers[5] = {10,20,30,40,50,60}; // compile time error at this line
    int i;
	
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
    return 0;
}

 

Leave a reply