C Programming One Dimensional Array with Examples

C Programming One Dimensional Array with Examples

One dimensional array in c programming; Through this tutorial, you will learn about One Dimensional Array in C with the help of examples.

One Dimensional Array in C with Examples

  • 1 D Array Definition in C
  • Rules For Declaring One Dimensional Array
  • 1 D Array Declaration in C
  • 1 D Initialization of C Array
  • 1 D Access Array Elements in C
  • Example 1 – One Dimensional Array in C
  • Example 2 – Program to print the largest and second largest element of the array in c

1 D Array Definition in C

One Deminsional 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.

Rules For Declaring One Dimensional Array

  • An array variable must be declared before being used in a program.
  • The declaration must have a data type(int, float, char, double, etc.), variable name, and subscript.
  • The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements.
  • An array index always starts from 0. For example, if an array variable is declared as s[10], then it ranges from 0 to 9.
  • Each array element stored in a separate memory location.

1 D 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.

1 D 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};

1 D 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 – One Dimensional Array in C

#include<stdio.h> 
int main() 
{ 
    int i; 
    int arr[5] = {10,20,30,40,50}; 
    // declaring and Initializing array in C 
    //To initialize all array elements to 0, use int arr[5]={0};    
    /* Above array can be initialized as below also 
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30; 
    arr[3] = 40;
    arr[4] = 50; */
    for (i=0;i<5;i++) 
    { 
        // Accessing each variable
        printf("value of arr[%d] is %d \n", i, arr[i]); 
    } 
}

Output:

Value of arr[0] is 10
Value of arr[1] is 20
Value of arr[2] is 30
Value of arr[3] is 40
Value of arr[4] is 50

Example 2 – 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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