×

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

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.