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 https://www.youtube.com/watch?v=xrqzVAnvCIY]

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

#include<stdio.h>

#define N 5
#define M (N * 2)

int main()
{
    int a[N], b[N], c[M], i, index = 0;

    printf("Enter %d integer numbers, for first array\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Enter %d integer numbers, for second array\n", N);
    for(i = 0; i < N; i++)
            scanf("%d", &b[i]);

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
    for(i = 0; i < N; i++)
        c[index++] = a[i];

    for(i = 0; i < N; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

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

#include<stdio.h>

#define N1 5
#define N2 6
#define M (N1 + N2)

int main()
{
    int a[N1], b[N2], c[M], i, index = 0;

    printf("Enter %d integer numbers, for first array\n", N1);
    for(i = 0; i < N1; i++)
        scanf("%d", &a[i]);

    printf("Enter %d integer numbers, for second array\n", N2);
    for(i = 0; i < N2; i++)
            scanf("%d", &b[i]);

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);
    for(i = 0; i < N1; i++)
        c[index++] = a[i];

    for(i = 0; i < N2; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

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