Implementing Arrays in C Programming
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.
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
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
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
- Single dimensional array
- Multidimensional array
Array declaration and initialization
You can declare the array and later initialize it.
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.
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
Example:
for (x=0; x<4;x++){ printf("Enter number %d \n", (x+1)); scanf("%d", &num[x]);}
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
Example-
for (int x = 0; x < 4; x++){ printf("num[%d] = %d\n", x, num[x]);}
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*/
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"); }
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.
- Either you store it in different variables. Suppose you have ten values to be stored. Will you be using ten different variables?
- 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.
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-
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:
If you want to access the elements of a 2-D array, use two subscripts:
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.
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
Comments
(1)
S
5 months ago
Report Abuse
Reply to Shaik Arif basha