Implementing Arrays in C Programming

Implementing Arrays in C Programming

5 mins read8.2K Views Comment
Updated on Dec 15, 2023 18:30 IST

In C programming, arrays are used to store multiple values of the same type in a single variable. To implement an array, you define its type and size, then initialize elements. For example, "int myArray[5];' creates an integer array with five elements. Access or modify elements using their index.

2022_05_Copy-of-What-is-1.jpg

An array is a very simple topic in the C programming language. But very important topic from the interview and exam point of view. In this blog, we will learn how to read and write data in an array and access arrays in C programming. We will also learn about multidimensional arrays.

Table of contents

  1. What is an Array?
    1. Array declaration and initialization
    2. Input data into the array
    3. Reading out data from an Array
  2. Programming example
  3. Multidimensional arrays

What is an Array?

An array is a variable containing multiple homogeneous values. Homogeneous values mean the same values. This means if we are declaring an array of integer type then only integer values will be there in an array. We can’t store floating values along with it. Arrays can only store a single series of data like-

  • Series of employee ID
  • Series of patient number
  • Number of items sold in a shop

In this, we can see that we have an array named Arr which is holding 6 values(0-5). If we write like float Arr[6]. This means it can hold six floating-point values.

NOTE: An array starts from index value 0.

Types of Array

  1. Single dimensional array
  2. Multidimensional array

 

Array declaration and initialization

You can declare the array and later initialize it.

2022_05_image-61.jpg

Here, an array of data-type floating points is declared with a size of 3. This means it can hold three integer-point values, or you can declare and initialize it at the same size.

 

Master Member Functions in C++
Master Member Functions in C++
If you are new to C++, it is crucial to learn about the Member function. With different types of member functions in C++ as well as the methods of declaration...read more
Class Vs. Object: What are the Differences?
Class Vs. Object: What are the Differences?
class is a blueprint or template for creating objects in object-oriented programming and bjects are the instances of class. In this article we will expore Class Vs. Object. We will...read more
C Strings with Examples
C Strings with Examples
C strings is a very important topic.This topic covers topics like assigning values to C string,reading C strings,passing c string to a function.

Input data into the array in C

If the array size is four, we iterate from  index=0 to index=3 in the array. We have written a printf statement(to display the output) in the For loop. So the message Enter number will print four times, and then we have written scanf statement(to input the values). This statement will ask the user to input the value. And now, that value entered by the user will be stored in an array.

Syntax:

 
for(int i=0;i<8;i++) scanf("%d",&array_name[i]);//if user want to input data
Copy code
Example:

 
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
Copy code

Also, explore: Implementing arrays in Java

Reading out data from an Array in C

Suppose we want to display the output then again, we have to use the For loop and write a printf statement in it.

Syntax:

 
for(int i = 0; i < 8; i++) { printf("%d ", array_name[i]); // print data of array with a space in between } printf("\n"); // move to the next line after printing all elements
Copy code
Example-

 
for (int x = 0; x < 4; x++)
{
printf("num[%d] = %d\n", x, num[x]);
}
Copy code

Access array elements

For accessing an array, you can use an index or array subscript. The array starts with index=0, which means arr[0] represents the first element in the array arr.

In the below example, there are n=15 elements in an array, then myarr[15-1], i.e. myarr[14], can be used to access an nth element of an array.  


 
int myarr[15];
printf("%d \n", myarr[0]); /* first element of array mydata*/
printf("%d \n", myarr[14]); /* last (14th) element of array mydata*/
Copy code

Programming example:

Write a C Program to input ten numbers into an array


 
#include <stdio.h>
int main()
{
int arr[10];
int i;
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
scanf("%d", &arr[i]);
}
printf("\nElements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
Copy code

Output :

Input 10 elements in the array :                                                                             
1
1
2
3
4
5
6
7
8
9
Elements in array are: 1  1  2  3  4  5  6  7  8  9 

Why use an array?

If you want to store one value ‘5’, you can use a single variable; let’s suppose ‘a’. But if in case you want to store multiple values? You will have two options.

  1. Either you store it in different variables. Suppose you have ten values to be stored. Will you be using ten different variables?
  2. The second option is using an array and storing the values in that array. Which method is better? Storing and accessing these values in a single array is less time-consuming than creating ten variables. 
  3.  
Default Arguments in C++
Default Arguments in C++
A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.This article is explaining...read more
Top C++ Interview Questions and Answers for 2024
Top C++ Interview Questions and Answers for 2024
C++ is a popular object-oriented programming (OOP) language. Due to its reliability, speed, compatibility, and performance, it is widely used in the development of operating systems, applications, games, servers, and...read more
All About Virtual Functions in C++
All About Virtual Functions in C++
In C++, a virtual function is a member function of a class that can be overridden by derived classes. It is declared as virtual in the base class and can...read more

Multidimensional arrays

Multidimensional arrays are an extended version of one-dimensional arrays. They are used to store data for 

  • Mathematical computations
  • Image processing
  • Record management. 

Other examples of a multidimensional array

  • Computer graphic transformation matrix
  • Pixel matrix of an image
  • Adjacency matrix in graph theory 

For example, If we have to represent a regular chessboard of size 8X8 on the computer, it can be easily represented using a multidimensional array. 

Two-dimensional array

The 2D array is organized as matrices which can be represented as the collection of rows and columns. It looks like this-

2022_05_image-60.jpg

This is an example of a two-dimensional array as we have two subscripts.

In a two-dimensional array, the first subscript, i.e. [4] as represents the number of rows, and the second (right) subscript, i.e. [3], represents the number of columns. We can represent the above two-dimensional array as follows:

2022_05_image-64.jpg

If you want to access the elements of a 2-D array, use two subscripts:

2022_05_image-63.jpg

Endnotes

I hope you understood about using Arrays in C programming. If you liked this blog please share it with other friends also.

FAQs

What is the difference between strings and arrays?

Arrays are all arrays. A string is a specific type of array with well-known rules for determining its length. In C, a string is just an array of characters (type char), with one drawback. Strings in C are always NUL-terminated. The "value" of the array is the same as the address (or pointer) of the first element. Therefore, C strings and pointers to char are often used interchangeably.

Is it possible to assign a different address to the array tag?

No. Array tags cannot be placed to the left of an assignment operator. ("Modifiable lvalues" are, of course, not "lvalues".) Arrays are objects. Array tag is a pointer to the first element of this object.

Do array indices always start at zero?

yes. Given an array a[MAX] (where MAX is a known value at compile time), the first element is a[0] and the last element is a[MAX-1]. This arrangement differs from that found in other languages. In some languages, e.g. in some versions of BASIC the elements will be a[1] to a[MAX] and in other languages, e.g. Pascal, you can have both.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio