Back to: C Tutorials For Beginners and Professionals
One Dimensional Array in C with Examples
In this article, I will discuss One Dimensional Array in C Language with Examples. Please read our previous articles discussing the basics of Array in C Language.
One Dimensional Array in C:
A one-dimensional array is an array with only one subscript specification needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.
Syntax: data-type arr_name[array_size];
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 s[10], it ranges from 0 to 9.
- Each array element is stored in a separate memory location.
Initialization of One-Dimensional Array in C
An array can be initialized at the following states:
- At compiling time (static initialization)
- Dynamic Initialization
Compiling time initialization:
The compile-time initialization means the array of the elements is initialized when the program is written or array declaration.
Syntax: data_type array_name [array_size]=(list of elements of an array);
Example: int n[5]={0, 1, 2, 3, 4};
Program:
#include<stdio.h>
int main()
{
int n[5]={0, 1, 2, 3, 4};
printf("%d", n[0]);
printf("%d", n[1]);
printf("%d", n[2]);
printf("%d", n[3]);
printf("%d", n[4]);
}
Output: 0 1 2 3 4
Run Time Initialization or an Array in C:
Run time initialization means the array can be initialized at runtime. That means array elements are initialized after the compilation of the program. For a better understanding, please observe the following example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[5], i;
printf ("Enter Array Elements:\n");
for (i = 0; i < 5; i++)
{
scanf ("%d", &a[i]); //Run time array initialization
}
printf ("Entered Array Elements are : ");
for (i = 0; i < 5; i++)
{
printf ("%d ", a[i]);
}
getch ();
}
Output:

Array declaration, initialization, and accessing
Array declaration syntax: data_type arr_name [arr_size];
Array initialization syntax: data_type arr_name [arr_size]=(value1, value2, value3,….);
Array accessing syntax: arr_name[index];
Example: Integer array example:
int age [5];
int age[5]={0, 1, 2, 3, 4};
age[0]; /*0 is accessed*/
age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/
Example: Character array example:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
str[0]; /*H is accessed*/
str[1]; /*a is accessed*/
str[2]; /*i is accessed*/
Example of 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:

Example to Take Input From User and Store in the Array in C:
Create an integer array with size 5, then take the values from the keyboard, store them in the array, and display the elements.
#include<stdio.h>
int main()
{
int arr[5];
int i;
printf("\n Enter the array elemnts : ");
for(i = 0; i<5; i++)
{
scanf("%d", &arr[i]);
}
printf("\n The array elemts are : ");
for(i = 0; i<5; i++)
{
printf(" %d ", arr[i]);
}
return 0;
}
Output:

Example:
Create an integer array with size 5 and then calculate the larger element of that array using the function.
#include<stdio.h>
int max(int arr[], int size)
{
int r, i;
r = arr[0];
for(i = 1; i<size; i++)
{
if (arr[i] > r)
r = arr[i];
}
return r;
}
int main()
{
int arr[5];
int m, i;
printf("Enter the array elements : ");
for(i = 0; i<5; i++)
scanf("%d", &arr[i]);
m = max(arr, 5);
printf("The largest element is : %d", m);
return 0;
}
Output:

In parameter creation, creating an entire array as an argument is impossible. In formal argument location, if we constructed array syntax, it only creates a pointer variable. In formal argument location, when we have int arr[] syntax, it creates a pointer variable only, and the syntax will indicate that it holds a single-dimensional array address.
Copying 1d arrays in C:
We have two arrays, list1 and list2
int list1[6] = {2, 4, 6, 8, 10, 12};
int list2[6];
and we want to copy the contents of list1 to list2. For general variables (e.g., int x=3, y=5), we use a simple assignment statement (x=y or y=x). But for arrays, the following statement is wrong.
list2 = list1;
We must copy between arrays element by element, and the two arrays must have the same size.
Example of Copying One Dimensional Array in C
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int source[MAX_SIZE], dest[MAX_SIZE];
int i, size;
/* Input size of the array */
printf("Enter the size of the array : ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements of source array : ");
for(i=0; i<size; i++)
{
scanf("%d", &source[i]);
}
/*
* Copy all elements from source array to dest array
*/
for(i=0; i<size; i++)
{
dest[i] = source[i];
}
/*
* Print all elements of source array
*/
printf("\nElements of source array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", source[i]);
}
/*
* Print all elements of dest array
*/
printf("\nElements of dest array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", dest[i]);
}
return 0;
}
Output:

Points to Remember About Array in C:
- An array is a derived data type in C constructed from the C language’s fundamental data type.
- An array is a collection of similar types of values in a single variable.
- When we require the ‘n’ number of a similar data type variable, we need to go for the array.
- When working with an array, memory will be created in the contiguous memory location to access the data randomly.
- In an array, all elements will share the same name with a unique identification value called index.
- The array index will always start from 0 and end with size – 1.
- Compile-time memory management will occur when working with an array, i.e., static memory allocation.
- The array size must always be an unsigned integer value, which should be greater than zero.
In the next article, I will discuss the Multi-Dimensional Array in C with examples. In this article, I try to explain One-Dimensional Array in C with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.
