×

Array Example in C

  • An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it.
  • It can store values that can be any number, but it must be of the same datatype. For example, if you want to store hundred (100) values of the same data type, they can be stored in a single array.
  • The data type that we declare in an array can be of any type like Char, Int, double, Float, or a String.

How to declare an Array?

Format for declaring an Array is

datatype NameoftheArray[SizeoftheArray];

For example: -

 1.int data [100];

 2. char data [5];

 3.double data [10];

 4.float data [15];

In the above examples, in first one we see that the (100) elements of the same data type, i.e., integer(int), are stored in a single array.

In the second one, five elements of the character(char) data type are stored in a single array.

In the same way, the double values and the float values are stored in a single array.

How to access the elements of Array?

  • Array elements are accessed by indices.
  • The Assessing of Array elements in the indices starts with 0, not with 1.
  • If you stored an element in an array as mentioned above, the first element would be stored in the 0th index in arr[0]. The second element will be stored in the 1st index arr[1], and so on.
  • If you want to initialize an array and store the values in the compile time, you can store the elements in an array as n, i.e., arr[n].
  • If the address of the first index that is stored in array arr[0] is 500, then the address of the second index stored in arr[1] will be 502, and the third index is 504, and so on. This is due to the size of the type of data type Int being 2 Bytes.
  • The address of the adjacent array element is increased depending upon the data type that is used in the array, like Int (2 Bytes), Float (4 bytes), double (16 bytes), etc.

Initialization of elements of an Array

The initialization of any array elements is done during the declaration of the array in the code.

The initialization of the array can be in any order. There is no rule that there should be an ascending or descending order. They can be in a zigzag manner.

Ex: -

int arr[10] = {1,54,19,76,24,98,26,7,13,77};

float arr[5] = {5.6, 4.9, 3.7, 9.2, 0.5};

An array can also be initialized without mentioning the size of the array, Only, if you have your own data elements.

Ex: -

int arr[] = {1,54,19,76,24,98,26,7,13,77};

float arr[5] = {5.6, 4.9, 3.7, 9.2, 0.5};

If you want to set the number of data elements in the compile time, then you can give the size of an array as n and run the code.

Input:

int arr[n] = new array;

Input and Output

Here's how you can take input from the user and store it in an array element.

taking the input and storing it in the 3rd position:

scanf("%d", &arr[2]);

taking the input and storing it in the ithe position:

scanf("%d", &arr[i-1]);

Here's how you can print an individual element of an array.

  • printing the first element of the array

printf("%d", arr[0]);

  • printing the third element of the array

printf("%d", arr[2]);

  • printing ith element of the array

printf("%d", arr[i-1]);

Examples of Array in C

Example-1: For INTEGER datatype

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

Output:-

Read and Print elements of an array:                                                                          
---------------                                                                    
Input 10 elements in the array :                                                                              
 0: 1                                                                                               
 1: 1                                                                                               
 2: 2                                                                                               
 3 : 3                                                                                               
 4: 4                                                                                               
 5: 5                                                                                               
 6: 6                                                                                               
 7: 7                                                                                               
 8: 8                                                                                               
 9: 9                                                                                               
                                                                                                              
Elements in array are: 1  1  2  3  4  5  6  7  8  9 

Example: 2 For FLOAT datatype

#include<stdio.h>
void  main()  
{  
    float arr[10]; 
    float i;  
       printf("\n\nRead and Print elements of an array:\n");
       printf("--------------\n");	
  
    printf("Input 10 elements in the array :\n");  
    for(i=0; i<10; i++)  
    {  
	    printf("%d : ",i);
              scanf("%d", &arr[i]);  
    }  
  
    printf("\nElements in array are: ");  
    for(i=0; i<10; i++)  
    {  
        printf("%d  ", arr[i]);  
    } 
    printf("\n");	
}

Output:-

Read and Print elements of an array:                                                                          
---------------                                                                   
Input 10 elements in the array :                                                                              
0: 1.1                                                                                             
1: 7.5                                                                                            
2: 9.7                                                                                            
3: 8.9                                                                                            
4: 9.3                                                                                            
5: 5.5                                                                                            
6: 1.0                                                                                            
7: 3.3                                                                                               
 8: 4.9                                                                                            
 9: 6.1                                                                                            
                                                                                                              
Elements in array are:  1.1, 7.5, 9.7, 8.9, 9.3, 5.5, 1.0, 3.3, 4.9, 6.1    

Example Code to reverse an Array

#include <stdio.h>


void main ()
  {
   int i,n,a[50];
   
       printf("\n Input  a number of values inside array and displaying it in the reverse order:\n");
       printf("----------------\n");
   
   printf("Input the number of elements  :");
   scanf("%d",&n);
   
   printf("Input %d number of elements :\n",n);
   for(i=0;i<n;i++)
      {
	  printf("%d : ",i);
	  scanf("%d",&a[i]);
	  }
      
   printf("\nThe values that are stored inside the array are: \n");
   for(i=0;i<n;i++)
     {
	   printf("% 5d",a[i]);
	 }
 
   printf("\n\nThe values that are stored inside the array reverse are   :\n");
   for(i=n-1;i>=0;i--)
      {
	   printf("% 5d",a[i]);
	  }
   printf("\n");
} 

Output:

Input  a number of values inside array and displaying it in the reverse order:
----------------
Input the number of elements :3
Input 3 number of elements :
0 : 2
1 : 5
2 : 7
The values that are stored inside the array are: 
    2    5    7


The values that are stored inside the array reverse are   :
    7    5    2

Related Topics

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.