Arrays in C programming with examples

C programming array; Through this tutorial, you will learn everything about array in c programming.

In other words, how to declare, initialize, access, and delete elements of an array in c programming with the help of examples.

Arrays in C programming

  • Array Definition in C
  • Array Declaration in C
  • Initialization of C Array
  • Access Array Elements in C
  • Example 1 – Program to print the largest and second largest element of the array in c
  • Example 2 – Program to find smallest and second smallest element in an array in c

Array Definition in C

An array is a variable that can hold multiple values or similar types of data. For example; an int array store the elements of int data type and a float array holds the elements of float data type, so on.

Note that:- An arrays can store the primitive type of data such as int, char, double, float. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float

Array Declaration in C

You can use the following syntax to declare an array in the c programming language; as shown below:

data_type array_name[array_size];  

Let’s see the following example for how to declare array in c programming; as shown below:

float mark[5];

Here, Mark is a float data type. It means it hold float types of value in it. And its size is 5.

Initialization of C Array

To initialize an array in c by using the index of each element. See the following easy way to initialize array in c programming; as shown below:

marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;  

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

Access Array Elements in C

Using array index, you can easily access any element stored in c array; as shown below: 

Let, you have declared an array named mark as above. So, you can access the element values in array by using array index; as shown below:

mark[0] /* first element of array mark*/
mark[4] /* last (4th) element of array mark*/

Example 1 – Program to print the largest and second largest element of the array in c

Use the following steps to write program to print largest and second largest element of the array in c:

  • Initialize and declare array size variable
  • Then initialize and declare the largest and second largest variable
  • Take array size or length from user in program
  • Take array elements or items input from the user in program
  • Define logic inside loop to find the largest and second largest elements from array
  • At the last, print largest and second largest elements from array in c
#include <stdio.h>
#include <stdlib.h>
int main()
{

int size,i,arr[50];//variable declaration for size of array

int large=0, secondLarge=0;

printf("Enter the size of array: ");

scanf("%d",&size);//taking input from user for number of elements in array

for(i=0; i<size; i++){

    printf("\n Enter the array element ");
    printf("%d :",(i+1));

    scanf("%d",&arr[i]);//Taking input for array elements

    if(large<arr[i]){
      secondLarge=large;
      large=arr[i];
    }
    else if(secondLarge<arr[i]){
    secondLarge=arr[i];
    }
}

//display result on the screen
printf("\n\n\n");
printf("The largest number in array is %d",large);
printf("\n The second largest number in array is %d",secondLarge);


return 0;
}

Output of the above c program; as shown below:

Enter the size of array: 5
Enter the array element 1 :321
Enter the array element 2 :123
Enter the array element 3 :654
Enter the array element 4 :456
Enter the array element 5 :987
The largest number in array is 987
The second largest number in array is 654

Example 2 – Program to find smallest and second smallest element in an array in c

Use the following steps to write program to print smallest and second smallest element of the array in c:

  • Initialize and declare array size variable
  • Then initialize and declare the smallest and second smallest variable
  • Take array size or length from user in program
  • Using for loop; take array elements or items input from the user in program
  • Implement loop and define logic inside loop to find the smallest and second smallest elements from array
  • At the last, print smallest and second smallest elements from array in c
#include <stdio.h>
#include <string.h>
void main()
{
 int smallest, secondsmallest;
 int array[100], size, i;

 printf("\n How many elements do you want to enter: ");

 scanf("%d", &size);

 for(i=0; i<size; i++){

    printf("\n Enter the array element ");
    printf("%d :",(i+1));

    scanf("%d",&array[i]);//Taking input for array elements

}

 for (i = 0 ; i < size; i++){

	 if (array[0] < array[1]) {
		 smallest = array[0];
		 secondsmallest = array[1];
	 }
	 else {
		 smallest = array[1];
		 secondsmallest = array[0];
	 }

	 for (i = 2; i < size; i++) {
		 if (array[i] < smallest) {
			 secondsmallest = smallest;
			 smallest = array[i];
		 }
		 else if (smallest == secondsmallest){
			 smallest = secondsmallest;
			 secondsmallest = array[i];
		 }
			 else if (array[i] < secondsmallest && array[i] > smallest) {
			 secondsmallest = array[i];
		  }
	 }
 }
 printf(" \nSecond smallest element is %d", secondsmallest);
 printf(" \nSmallest element is %d\n", smallest);
}

Output of the above c program; as shown below:

How many elements do you want to enter: 5
Enter the array element 1 :2
Enter the array element 2 :6
Enter the array element 3 :4
Enter the array element 4 :8
Enter the array element 5 :5
Second smallest element is 4
Smallest element is 2

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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