Linux C Programming Tutorial Part 7: Arrays

So we have already discussed the concept of variables in C programming (here and here). To quickly refresh, a variable is something which can hold a value of a particular type - it could be an integer, character, or even floating point. However, there's one limitation of variables: they can only hold a single value at any given time.

This means if you want to hold several values, you'll have to deal with equal number of variables. For example, if you want a program to accept 10 integer values from user and then output them, then either you'll write it in this way:

#include <stdio.h>

int main()
{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;
printf("Enter 10 integer values\n");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
scanf("%d",&e);
scanf("%d",&f);
scanf("%d",&g);
scanf("%d",&h);
scanf("%d",&i);
scanf("%d",&j);

printf("You entered the following values:\n");
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
printf("%d\n",e);
printf("%d\n",f);
printf("%d\n",g);
printf("%d\n",h);
printf("%d\n",i);
printf("%d\n",j);


return 0;
}

Or in the following way:

#include <stdio.h>

int main()
{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;

printf("Enter 10 integer values\n");
scanf("%d %d %d %d %d %d %d %d %d %d",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);

printf("You entered the following values:\n");
printf("%d %d %d %d %d %d %d %d %d %d\n",a,b,c,d,e,f,g,h,i,j);


return 0;
}

The second approach may be slightly better if you compare in terms of lines of code written, but there's still a better way, and that is to use an integer array.

Yes, if there's a requirement to hold multiple values of same type, then you should opt for an array. Here's how an integer array capable of holding 10 values is defined:

int arr[10]

So here, 'int' refers to the type of values array 'arr' will store. The number 10 in square brackets tells the capacity of 'arr' is 10 values. Now, here's a piece of code using this array:

#include <stdio.h>

int main()
{
int arr[10],i;

printf("Enter 10 integer values\n");

for(i=0;i<10;i++)
scanf("%d",&(arr[i]));

printf("You entered the following values:\n");

for(i=0;i<10;i++)
printf("%d\n",arr[i]);


return 0;
}

Note that we've already discussed scanf and printf functions in one of our earlier tutorials, so you can refer to those articles in case you want to know more about them.

Coming back to arrays, you'd have observed the 'for' loop in the code above begins with i=0. That's because array subscripts (the number inside square brackets)  always start from zero. So here, in our case, arr[0] refers to the first value entered by the user, arr[1] refers to the second value, and so on until arr[9] which refers to the 10th value.

It's also worth mentioning that you can easily access the memory address of each value stored in the array. This can be done using the array name, which is 'arr' in this case. So if you use just 'arr' in the code, then it's nothing but the memory address of the first element.

Similarly, if you want to access the memory address of the second element, just use 'arr+1'. For third element, use 'arr+2', and so on and so forth. The following piece of code and its output should give you a better idea.

#include <stdio.h>

int main()
{
int arr[10],i;

printf("The address of first and second values are: %u %u\n", arr, arr+1);

return 0;
}

And here's the output:

The address of first and second values are: 2904035888 2904035892

As you can see, there's a difference of 4 bytes in these addresses - that's because one integer value occupies 4 bytes on my system. 

Conclusion

Here, in this tutorial, we discussed the basics of arrays. Note that we only gave example of an integer array. You can have array of other types as well, like characters. Try out the examples we've used in this tutorial, and let us in know in the comments section below if you have any doubt or query.

Share this page:

0 Comment(s)