C Arrays – 1D (1 Dimensional)
An array in C is a collective name given to a group of similar variables.
C arrays can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will discuss 1-Dimensional (1D) arrays in C. So lets start with 1D array in C.
Let us understand C arrays with the help of an example.
void main(){ int arr[3],i; printf(“Enter 3 values\n”); for(i=0;i<3;i++) { scanf(“%d”,&arr[i]) } printf(“The entered values are:\n”); for(i=0;i<10;i++) { printf(“%d\t”,arr[i]) } }
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 in C is declared, a garbage value is stored in it. Thus, accessing the value of array elements without defining it will give garbage value.
for(i=0;i<3;i++) { scanf(“%d”,&arr[i]) }
The above for loop accepts the value from the user and stores it in an array. The 1D array in C 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]
for(i=0;i<3;i++) { printf(“%d\t”,arr[i]) }
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. C array element can only hold one type of data i.e. either integer or float or character.
Consider this program,
void main(){ int arr[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; }
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}
If you want to learn about 2D arrays in C, visit 2-Dimensional arrays
learnconline rockzzzzzzzzzzzzzz
thanks a lot 🙂 real good explanation.
it has made the concept vry clr
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?
thanks!
@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.
Thank you!
reallu very helpful for newly learners
so far, it feels like math class all over again
An Array is a Common name Which allocate Contiguous memory Allocation in System
Nishat Anwar
Really Good explanation
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
thanks dear
Thanks buddy, this site is proving a great help. My college notes and lecturers aren’t unfortunately!
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.
Nice example. Thanks
helpful it is!!
🙂
why index value should necessarily start from 0 why not 1????????
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.
awesome explanation
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!
Thanks a lot its really great help to do programs.
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
learn c online is so helpfull for learn ‘c’ thanx the devlopers….
Good
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++;
}
}
}
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];
}
relly helpful