Copy An Array into another Array In C
C Programming Language Arrays

Easy Program To Copy An Array into another Array In C

This article is all about how to write a program to Copy an array in c. If you are searching for this program then you are at the right place. So without wasting your time let’s start creating the program to copy an array to another.

Copy An Array into another Array In C:

We have given 5 to size by #define means that the size of an array will be the same throughout the program. In the main function, we declare integer type arrays of size 5. Now we take the elements of 1st array from the user. So, we use for loop to take the elements.

‘i’ starts with zero because the index number of the array always starts with zero and the condition is that ‘i’ is less than the size means that loop will be executed until i will not equal to 5. If i is equal to 5 then the condition if false and it will move out from the body of for loop.

Until the condition is false the compiler will ask for the elements of an array from the user. So, after scanning or taking the elements from the user we use another for loop that is for printing the elements of array one. The second for loop will print the array one.

And now the third loop is the main loop where we will assign the value of array one to array two. But the question is how? We simply write array2[i]=array1[i] which will assign the value of array 1 to array 2. In the last or fourth loop, we print array 2 which will be equal to array 1 if array 2 is not giving a proper or correct answer it means there is some mistake in your source code.

Example Of Copying An Array:

#include<stdio.h>
#define size 5
int main(){
int a[size],b[size],i;
printf("ENTER %d ELEMENTS OF ARRAY :\n",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
//printing elements of array 1//
printf("ELEMENTS OF ARRAY 1 ARE ");
for(i=0;i<size;i++){
printf("%d,",a[i]);
}
for(i=0;i<size;i++){
b[i]=a[i];
}
printf("\nELEMENTS OF ARRAY 2 ARE ");
for(i=0;i<size;i++){
printf("%d,",b[i]);
}
return 0;
}

Output:

Copying An Array In C
Copying An Array into another Array In C

 

 

 

 

Conclusion:

From the above program, we concluded how to write a c program to copy an array into another array, and from the above image, you will clearly see the correct output of the program. If you are searching for the loops program or examples click on the Loops. So, this is all about our today’s example of how to copy an array in c?

Leave a Reply

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