BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Arrays in C programming with examples

Last Updated: September 24, 2017 by Chaitanya Singh | Filed Under: c-programming

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.
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 (we will discuss that later in this tutorial).

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;
    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:

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

Lets discuss the important parts of the above program:

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]);
}

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

c-arrays

More Topics on Arrays in C:
2D array – We can have multidimensional arrays in C like 2D and 3D array. However the most popular and frequently used array is 2D – two dimensional array. In this post you will learn how to declare, read and write data in 2D array along with various other features of it.

Passing an array to a function– Generally we pass values and variables while calling a function, likewise we can also pass arrays to a function. You can pass array’s element as well as whole array (by just specifying the array name, which works as a pointer) to a function.

Pointer to array – Array elements can be accessed and manipulated using pointers in C. Using pointers you can easily handle array. You can have access of all the elements of an array just by assigning the array’s base address to pointer variable.

❮ PreviousNext ❯

Top Related Articles:

  1. C – Decision control statements in C programming language
  2. Structure in C programming with examples
  3. User-defined function in C with Examples
  4. Function call by reference in C Programming
  5. C – Pointer to Pointer (Double Pointer) with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. gosego says

    May 26, 2016 at 10:23 AM

    need help!i want to define a structure named student containing the fields “name” and “CA”,then declare an array of structure having 50 elements of student type.Using the array i would then like to display the name and CA of student number 11…..HELP!

    Reply
    • Avishek says

      June 8, 2017 at 5:23 PM

      You can declare the range of array after scanf function. You can use string to store name. For that you have to declare the array in char data type.

      Reply
  2. Ayushi Verma says

    September 28, 2016 at 6:31 PM

    I really like the lucid language you use and the flow of teaching is awesome. Thank you for creating this website.

    Reply
  3. Shedrack says

    July 4, 2017 at 12:44 PM

    I appreciate your teaching so much. Thanks for the info.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap