0% found this document useful (0 votes)
0 views5 pages

Basics of Programming - C Arrays

C arrays are data structures that store a fixed-size collection of elements of the same type, allowing for efficient data management. They can be declared for various data types and accessed using an index, with examples provided for calculating the average of integers. The document also covers initialization methods and memory representation of arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
0 views5 pages

Basics of Programming - C Arrays

C arrays are data structures that store a fixed-size collection of elements of the same type, allowing for efficient data management. They can be declared for various data types and accessed using an index, with examples provided for calculating the average of integers. The document also covers initialization methods and memory representation of arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 5

Basics of Programming – C Arrays 1

C - Arrays
Arrays are a kind of data structure that can store a fixed-size sequential collection of elements
of the same type. An array is used to store a collection of data, but it is often more useful to
think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables. A specific element in an array is accessed by an
index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

An array is a group (or collection) of same data types. For example an int array holds the
elements of int types while a float array holds the elements of float types.

Why we need Array in C Programming?


Consider a scenario where you need to find out the average of 100 integer numbers entered by
user. In C, you have two ways to do this:

1) Define 100 variables with int data type and then perform 100 scanf() operations to store the
entered values in the variables and then at last calculate the average of them.

2) Have a single integer array to store all the values, loop the array to store all the entered
values in array and later calculate the average.
Basics of Programming – C Arrays 2

Which solution is better according to you?

Obviously the second solution, it is convenient to store same data types in one single variable
and later access them using array index.

How to declare Array in C


int num[35]; /* An integer array of 35 elements */

char ch[10]; /* An array of characters for 10 elements */

Similarly an array can be of any data type such as double, float, short etc.

How to access element of an array in C


You can use array subscript (or index) to access any element stored in array. Subscript starts
with 0, which means arr[0] represents the first element in the array arr.

In general arr[n-1] can be used to access nth element of an array. where n is any integer
number.

For example:

int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*/

Example of Array In C programming to find out the average of 4


integers
#include <stdio.h>
int main()
{
int avg = 0;
Basics of Programming – C Arrays 3

int sum =0;


int x=0;

/* Array- declaration – length 4*/


int num[4];

/* We are using a for loop to traverse through the array


* while storing the entered values in the array
*/
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
for (x=0; x<4;x++)
{
sum = sum+num[x];
}

avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}

Output:

number 3
20
Enter Enter number 1
10
Basics of Programming – C Arrays 4

Enter number 2
10
Enter number 4
40
Average of entered number is: 20

Input data into the array


Here we are iterating the array from 0 to 3 because the size of the array is 4. Inside the loop we
are displaying a message to the user to enter the values. All the input values are stored in the
corresponding array elements using scanf function.

for (x=0; x<4;x++)


{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}

Reading out data from an array


Suppose, if we want to display the elements of the array then we can use the for loop in C like
this.

for (x=0; x<4;x++)


{
printf("num[%d]\n", num[x]);
}
Basics of Programming – C Arrays 5

Various ways to initialize an array


In the above example, we have just declared the array and later we initialized it with the values
input by user. However you can also initialize the array during declaration like this:

int arr[5] = {1, 2, 3, 4 ,5};

OR (both are same)

int arr[] = {1, 2, 3, 4, 5};

Un-initialized array always contain garbage values.

C Array – Memory representation

You might also like