An array is a group of data objects of the same kind kept nearby in ram. Inside the C programming, arrays are indeed a derivative type of data that may hold primitive data types like int, char, double, float, etc. It may also hold a group of derivative data types like pointers, structures, and so forth. Whenever you wish to record a student’s grades in six courses, we shouldn’t need to create separate variables for each subject’s grades. Alternatively, we may create an array that could hold the marks for every topic in shared memory regions. We may simply retrieve the items by utilizing the array. To retrieve the array’s members, just a few other lines of the C script are necessary. Let’s take a glance at some instances to see the working of arrays in C language. When writing this tutorial, we have been using the Ubuntu 20.04 operating System to elaborate arrays.

Example 01:

Let’s start with opening the terminal shell in Ubuntu 20.04 operating system. Use “Ctrl Alt T” to open the shell console within 5 seconds. Let’s begin with creating a C language file using the “touch” query as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-681.png" data-lazy- height="19" src="data:image/svg xml,” width=”420″>

Open it in some editor to add code to it. Hence, we have been using the “nano” query to open this file in the GNU.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-682.png" data-lazy- height="20" src="data:image/svg xml,” width=”410″>

Now the file has been opened in the GNU Editor; it’s time to write C code to elaborate arrays. It would help if you recognized that there are diverse ways to declare an array and initialize it. We will be discussing the first and simplest way to declare and initialize an array in C language in this example. So, add the input-output standard library first. After this, start the main function in the code. We have affirmed an array of magnitude 5. After this, we have added values to the array at every index separately. After that, we have used the “for” loop to print all 5 elements of an array “arr” in the shell. This is one simplest way to state and formerly make ready an array. Save your code and quit it using “Ctrl S” and “Ctrl X” one after another.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-683.png" data-lazy- height="293" src="data:image/svg xml,” width=”434″>

Now the file has been saved, we have to compile the code to make it an executable file. So, use the “gcc” c compile to compile it as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-684.png" data-lazy- height="21" src="data:image/svg xml,” width=”403″>

Let’s just run the file to see the output of a C code of array. We have to use the represented instruction in the console and tap on the “Enter” key to do that. The output shows all 5 values of the array one after another.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-685.png" data-lazy- height="102" src="data:image/svg xml,” width=”417″>

Example 02:

Let’s look at a new instance to declare and initialize an array with a different method this time. Hence, open your file “test.c” again.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-686.png" data-lazy- height="22" src="data:image/svg xml,” width=”414″>

As the file has been opened in the editor, add the input-output standard library first. The main function has been used as same as above. In this example, we have declared two arrays “odd” and “even” to initialize odd and even values in arrays consequently. The array “odd” has no size defined and has 5 elements initialized with odd values. The print message has been displayed to show the message before printing odd elements. For loop is used to collect the values from the index of array “odd” and print one by one. The array “even” contains 5 even numbers and a defined size of 5. The print message will be displayed before the “for” loop to show that the even numbers will be shown. Then, the “for” loop has been used to print the values of even arrays one by one.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-687.png" data-lazy- height="328" src="data:image/svg xml,” width=”448″>

Compile the “test.c” file yet again to execute it further.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-688.png" data-lazy- height="18" src="data:image/svg xml,” width=”422″>

After the compilation of a code, execute it using the below command. The output will be as revealed in the lower image. It will print odd array elements first, then even array elements and simple text messages before both arrays.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-689.png" data-lazy- height="234" src="data:image/svg xml,” width=”397″>

Example 03:

Let’s see an example of an array taking value from user input and then display those values in the terminal. So, open the same file once more.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-690.png" data-lazy- height="18" src="data:image/svg xml,” width=”415″>

The file is opened in the GNU nano editor now. We have added the below C script in it to take user input. After including the standard input-output header and the main function, we have declared an array “store” of size 7. The print statement has been utilized to print a message for the user to add only integer values. The “for” loop has been used to collect and add the user inputted values to the array “store” via & sign in the scanf function. After the “for” loop has been ended, another display message has been printed out. The second “for” loop has been used to display the array values on the screen via the “printf” statement. The main function closes with a return 0 phrase. Save this code and leave.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-691.png" data-lazy- height="314" src="data:image/svg xml,” width=”457″>

Now use the “gcc” query to compile this code.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-692.png" data-lazy- height="19" src="data:image/svg xml,” width=”419″>

As the code has been compiled, run via the below-shown query. The consumer has been requested to pass in the integer type values. You can see we have added 7 values and pressed Enter. After the display message, all the values added by the user would be saved into an array and then printed out.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-693.png" data-lazy- height="182" src="data:image/svg xml,” width=”423″>

Example 04:

Let’s take an example to calculate sum and average from the array contents. Open your C file first.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-695.png" data-lazy- height="19" src="data:image/svg xml,” width=”412″>

After the opening of a file, add the below code same to same in it. Again we have used the standard C library and main function first. Array “Arr” has been affirmed of size 10. Two integer variables: sum and avg, have been declared to count total and average from array elements. The print statement is used to ask the user to add integer values. The “for” loop is used to add the user-inputted values to the array “Arr” using the scanf method. Meanwhile, all the values added by the user will be summed up together and saved to variable “sum”. After this sum is displayed, and the average will be displayed after calculation.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-697.png" data-lazy- height="291" src="data:image/svg xml,” width=”442″>

Compile your code.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-699.png" data-lazy- height="19" src="data:image/svg xml,” width=”412″>

The output is shown below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-701.png" data-lazy- height="73" src="data:image/svg xml,” width=”424″>

Example 05:

In this illustration, we will discuss how to sort an array. Open file to do that.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-703.png" data-lazy- height="17" src="data:image/svg xml,” width=”424″>

An integer variable “temp” and array “Arr” of size 10 have been initialized. The first “for” loop is used to print the current order of an array. The second and third “for” loops have been used to sort the existing array in ascending order using swapping. If the statement contains the condition that if the “i 1” index value is greater than the index value, add the index “I” value to variable temp. Add the values of index “i 1” to index “I”. Now add the variable temp value to index “i 1”. The fourth “for” loop is used to print the sorted array.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-705.png" data-lazy- height="420" src="data:image/svg xml,” width=”628″>

Compile the file.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-707.png" data-lazy- height="17" src="data:image/svg xml,” width=”407″>

The output below shows the unsorted array first; then, the array has been sorted and displayed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-708.png" data-lazy- height="431" src="data:image/svg xml,” width=”493″>

Conclusion:

The fundamental data structure would be an array that allows every data object to be retrieved arbitrarily by using an index value. If you need to save comparable items, a C array is useful

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg6102f8c1b79e7.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.