C Program To Concatenate Two Arrays

Lets write a C program to concatenate or append two arrays into a third array. Make use of macros to assign size of the arrays.

Example: Expected Output

Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 5 integer numbers, for second array
5
6
7
8
9

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
1
2
3
4
5
6
7
8
9
Concatenation of arrays

Video Tutorial: C Program To Concatenate Two Arrays



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

Source Code: C Program To Concatenate Two Arrays

Method 1: Arrays with same size

  1. #include<stdio.h>  
  2.   
  3. #define N 5  
  4. #define M (N * 2)  
  5.   
  6. int main()  
  7. {  
  8.     int a[N], b[N], c[M], i, index = 0;  
  9.   
  10.     printf("Enter %d integer numbers, for first array\n", N);  
  11.     for(i = 0; i < N; i++)  
  12.         scanf("%d", &a[i]);  
  13.   
  14.     printf("Enter %d integer numbers, for second array\n", N);  
  15.     for(i = 0; i < N; i++)  
  16.             scanf("%d", &b[i]);  
  17.   
  18.     printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);  
  19.     for(i = 0; i < N; i++)  
  20.         c[index++] = a[i];  
  21.   
  22.     for(i = 0; i < N; i++)  
  23.         c[index++] = b[i];  
  24.   
  25.     printf("\nElements of c[%d] is ..\n", M);  
  26.     for(i = 0; i < M; i++)  
  27.         printf("%d\n", c[i]);  
  28.   
  29.     return 0;  
  30. }  

Output:
Enter 5 integer numbers, for first array
0
9
8
7
6
Enter 5 integer numbers, for second array
5
4
3
2
1

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
9
8
7
6
5
4
3
2
1

Logic To Concatenate Two Array To Form Third Array

Here size of array a and b are same, so the for loops are same to accept elements of array a and b. We initialize variable index to 0. We write a for loop and iterate from 0 to 4, and copy the individual elements of array a to array c. Here index of array a and c varies from 0 to 4.

In the second for loop again we iterate the for loop from 0 to 4. This time value of variable index varies from 5 to 9, where as value of index of variable b varies from 0 to 4. So the individual elements of array b are copied to array c from index 5 to 9.

Method 2: Arrays with different size

  1. #include<stdio.h>  
  2.   
  3. #define N1 5  
  4. #define N2 6  
  5. #define M (N1 + N2)  
  6.   
  7. int main()  
  8. {  
  9.     int a[N1], b[N2], c[M], i, index = 0;  
  10.   
  11.     printf("Enter %d integer numbers, for first array\n", N1);  
  12.     for(i = 0; i < N1; i++)  
  13.         scanf("%d", &a[i]);  
  14.   
  15.     printf("Enter %d integer numbers, for second array\n", N2);  
  16.     for(i = 0; i < N2; i++)  
  17.             scanf("%d", &b[i]);  
  18.   
  19.     printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);  
  20.     for(i = 0; i < N1; i++)  
  21.         c[index++] = a[i];  
  22.   
  23.     for(i = 0; i < N2; i++)  
  24.         c[index++] = b[i];  
  25.   
  26.     printf("\nElements of c[%d] is ..\n", M);  
  27.     for(i = 0; i < M; i++)  
  28.         printf("%d\n", c[i]);  
  29.   
  30.     return 0;  
  31. }  

Output:
Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 6 integer numbers, for second array
5
6
7
8
9
10

Merging a[5] and b[6] to form c[11] ..

Elements of c[11] is ..
0
1
2
3
4
5
6
7
8
9
10

Here the logic to concatenate array elements is same, but only difference is the size of array variable a and b. So we need to be careful while writing conditions in for loop.

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