1-Dimensional (1D) Array in C Programming Language

An array is a collective name given to a group of similar variables.

An array can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will discuss 1-Dimensional arrays in C Programming Language. So lets start with 1-Dimensional array.

Let us understand this with the help of an example.

Explanation:
int arr[3] statement declares an array capable of holding 3 integer values. The first value can be accessed using arr[0]. Similarly second value can be accessed using arr[1]. Note that the number enclosed in [] is called index value. The index value of array in C always starts from 0.
So,
First element –> arr[0]
Second element –> arr[1]
Third element –> arr[2]
nth element –> arr[n-1]

When an array is declared, a garbage value is stored in it. Thus, accessing the value of array elements without defining it will give garbage value.

The above for loop accepts the value from the user and stores it in an array. The array that was declared before is getting defined here.

Thus,
First value corresponding to 1st iteration of for loop is stored in the element arr[0]
Second value corresponding to 2nd iteration of for loop is stored in the element arr[1]
Third value corresponding to 3rd iteration of for loop is stored in the element arr[2]

The above for loop displays the value stored in the array.

Thus,
1st iteration displays value stored in arr[0]
2nd iteration displays value stored in arr[1]
3rd iteration displays value stored in arr[2]

In the above program, we have used data type int for the array. Hence it is called integer array. We can also declare array as float. But then it will contain float values. An array element can only hold one type of data i.e. either integer or float or character.

Consider this program,

Let us suppose that base address of this array is 6000. So value 1 is stored at memory location 6000. Since it is integer array, 16 bit C compiler will assign 2 bytes of memory for its storage. Hence next value i.e. 2 will be stored at location 6002. Similarly, 3 will be stored at location 6004.

An array can be defined as follows:
int arr[3]={1,2,3};
i.e., arr[0] –> 1
arr[1] –> 2
arr[2] –> 3

An array can also be defined as:
int arr[]={1,2,3}

You may also like...

29 Responses

  1. Anonymous says:

    learnconline rockzzzzzzzzzzzzzz

  2. Anonymous says:

    thanks a lot 🙂 real good explanation.
    it has made the concept vry clr

  3. Anirudh says:

    In the example given, you are letting the second for loop run till i equals 9.
    So does this mean that the output for index values after 2 i.e. arr[3],arr[4],…..,arr[9] will all be garbage values since your are not assigning any values to them?

  4. Anonymous says:

    thanks!

  5. LearnCOnline says:

    @Anirudh:
    Yes… arr[3], arr[4]…..arr[9] will hold garbage value as we are not assigning the values to these array elements. Printing those array values will result in garbage data.

  6. Anonymous says:

    reallu very helpful for newly learners

  7. Anonymous says:

    so far, it feels like math class all over again

  8. Nishat Anwar says:

    An Array is a Common name Which allocate Contiguous memory Allocation in System

    Nishat Anwar

  9. Meadow says:

    Really Good explanation

  10. main()
    {int i;
    float x[10],value,total;
    printf(“enter 10 real number”);
    for(i=0;i<10;i++)
    {
    scanf(“%f”,&value);
    x[i]=value;
    }
    total=0.0;
    for(i=0;i<10;i++)
    total=total+x[i]*x[i];
    for(i=0;i<10;i++)
    printf(“x[%2d]=%5.2f\n”,i+1,x[i]);
    printf(“\ntotal=%2f\n”,total);
    }

    i m nw to c bt i found when i do total=x[i]*x[i]
    give another ans 102.. bt when i add total it gives me 446.86.plz help me

  11. Anonymous says:

    thanks dear

  12. Anonymous says:

    Thanks buddy, this site is proving a great help. My college notes and lecturers aren’t unfortunately!

  13. Anonymous says:

    respected sir or madam.i would be a great pleasure if u let me know about memory locations clearly.
    thanks a lot for this site very very much.it can’t be expressed in words to say how great this is helping to learn c.thank you very much for your effort helping many people like me.please kindly reply at your earliest possible.

  14. Anonymous says:

    Nice example. Thanks

  15. Anonymous says:

    helpful it is!!
    🙂

  16. Anonymous says:

    why index value should necessarily start from 0 why not 1????????

  17. Jesse Strode says:

    Simply, people start counting at 1 and computers start counting at 0.
    Similarly, we write 0, 1, 2, 3, 3, 5…etc. Computers write 0, 1, 10, 11, 100, 101… etc. index values are computer language, not people language, unfortunately.

  18. Anonymous says:

    awesome explanation

  19. Anonymous says:

    thanks alot! i really need this. i wish you were happy with your life and always success! i love you, i love you all! thanks for this man. i meant it. really!

  20. Shanthi says:

    Thanks a lot its really great help to do programs.

  21. vasanth says:

    Hi there, I am new one to ‘C’,for embedded C am learning C. It’s very use full site for me, till the pointers i can read and easily understood after the pointers there is a small confusion for me to learn.if u will update this website according to me please try to explain from the pointers very clearly then you will the number one online c teacher

  22. sonu says:

    learn c online is so helpfull for learn ‘c’ thanx the devlopers….

  23. MOHAMMAD ANAS ANSARI says:

    Good

  24. Li Long says:

    Can you guys help me with this assignment please.
    1. Declare an array arr1 and initialize it to 1, 2, 3, 4, 5, 6, 7, 8, and 9
    (From this point on, use the variable length instead of a number to define the capacity
    of the array.)
    2. Declare another array arr2, same length of arr1, without entering any integers.
    3. Using a for loop print arr1 displaying all elements on one line, separated by a comma
    (commas must be placed only between numbers).
    4. Using a while loop, print arr2 displaying all elements on one line, separated by a dash
    (dashes must be placed only between numbers).
    5. Using a for loop, copy all the values of arr1 into arr2 in reverse order.
    6. Using a while loop copy all elements of arr1 into arr2 in reverse order.
    7. Using a do/while loop print arr1 displaying all elements on one line, separated by a
    comma.
    8. Using a for loop, print arr2 displaying all element on one line, separate by a dash.

    i have this so far:

    public class Array
    {
    public static void main(String[] args)
    {
    int Array1[] = { 1,2,3,4,5,6,7,8,9};
    int Array2[] = new int [Array1.length];

    printArray1(Array1);
    printArray2(Array2);
    }

    public static void printArray1(int[] Array1)
    {
    System.out.print(“Array 1: “);
    for(int i=0; i < Array1.length; i++)
    System.out.print(Array1[i] +",");
    System.out.println();
    }
    public static void printArray2(int[] Array2)
    {
    System.out.print("Array 2: ");
    int i = 0;
    while ( i <= Array2.length – 1){
    System.out.print( Array2[i]+ "-");
    i++;
    }
    }
    }

    • learnconline says:

      5. Using a for loop, copy all the values of arr1 into arr2 in reverse order.
      int arr1[5]={2,1,4,5,6};
      int arr2[5];
      int i,j;
      for(i = 0, j = 4; i < 5; i++, j–){
      arr2[j] = arr1[i];
      }

  25. Mahendra Bohara says:

    relly helpful

  1. March 1, 2014

    […] 1-Dimensional Array […]

  2. September 6, 2016

    […] 1-Dimensional (1D) Array […]

Leave a Reply

Your email address will not be published. Required fields are marked *