LERAN C PROGRAMMING

Learn one of the most powerful programming languages in the world and become a rockstar developer.

Arrays


In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access array elements of an array with the help of examples.

C Programming Arrays

An array is a variable that can store multiple values. For example, if you want to store 10 integers, you can create an array for it.

   int data[10]; 

Decleration of array:

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows −

   dataType arrayName[arraySize];

For example:

   int data[10];

Here data is a variable array which is sufficient to hold up to 10 integer numbers.

Initialize Array:

It is possible to initialize an array during declaration. For example,

   int data[5] = {10, 4, 6, 7, 2};

You can also initialize the array as below:

   int data[] = {10, 4, 6, 7, 2};

In above example, We havenot declare the size of array. In such condition, compiler automatically know the size of array.

Access Array Elements:

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example −

   int adzetech = data[3];

The above statement will take the 4th element from the array and assign the value to adzetech variable.

Few keynotes:

  • Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
  • If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
  • Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
    This is because the size of a float is 4 bytes.

Changing array value:

The of array can be changed by using position. Lets take an example:-

int data[5] = {10, 4, 6, 7, 2}; // our array
data[3]=5; // this means it assign the 4th position value to 5

Input array element:

You can take input from user to assign the value as shown in example:-

// take input and store it in the 3rd element
scanf("%d", &data[2]);

Output array element:

You can print output individually from array as shown in example:-

// print the first element of the array
printf("%d", data[0]);

Example 1 :

// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>
int main() {
  int values[5];
  printf("Enter 5 integers: ");
  // taking input and storing it in an array
  for(int i = 0; i < 5; ++i) {
     scanf("%d", &values[i]);
  }
  printf("Displaying integers: ");
  // printing elements of an array
  for(int i = 0; i < 5; ++i) {
     printf("%d\n", values[i]);
  }
  return 0;
}
Output :

Enter 5 integers: 1
2
5
-3
5
Displaying integers: 1
2
5
-3
5

Here, we have used a for loop to take 5 inputs from the user and store them in an array. Then, using another for loop, these elements are displayed on the screen.

Multidimensional arrays

In this tutorial, you learned about arrays. These arrays are called one-dimensional arrays.

In the next tutorial, you will learn about multidimensional arrays (array of an array).



Previous Next
x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine