C Program To Copy Elements of One Array To Another


Lets write a c program to copy all the elements of one array to another array of same size.

Related Read:
Basics of Arrays: C Program

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
2 –> 2
6 –> 6
4 –> 4
3 –> 3
Copy array elements from a to b

Video Tutorial: C Program To Copy Elements of One Array To Another



YouTube Link: https://www.youtube.com/watch?v=ib_xqm4J1d0 [Watch the Video In Full Screen.]

Source Code: C Program To Copy Elements of One Array To Another

  1. #include<stdio.h>  
  2.   
  3. #define N 5  
  4.   
  5. int main()  
  6. {  
  7.     int a[N], b[N], i;  
  8.   
  9.     printf("Enter %d integer numbers\n", N);  
  10.     for(i = 0; i < 5; i++)  
  11.         scanf("%d", &a[i]);  
  12.   
  13.     printf("\n\nCopying elements of array a to b\n");  
  14.     for(i = 0; i < N ; i++)  
  15.         b[i] = a[i];  
  16.   
  17.     printf("\nOriginal(a[%d])  -->  Copy (b[%d])\n", N, N);  
  18.     for(i = 0; i < N; i++)  
  19.         printf("%4d\t\t-->%6d\n", a[i], b[i]);  
  20.   
  21.     return 0;  
  22. }  

Output:
Enter 5 integer numbers
5
4
3
2
1

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
4 –> 4
3 –> 3
2 –> 2
1 –> 1

Logic To Copy Elements of One Array To Another

We ask the user to enter N integer numbers. N is macro which is used to define size of the array. We store the user entered numbers inside array variable a. Since both array variables a and b has same size we copy individual elements of array variable a to array variable b at same index position.

At the end we print / display the content of both original array(a[5]) and the array to which the elements are copied to(b[5]).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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