C program to copy all elements of one array to another

Write a C program to read elements in array and copy all elements of first array into second array. C program to copy elements of array.

Required knowledge:

Basic C programming, For loop, Array

Program:

/**
 * C program to copy one array to another array
 */

#include <stdio.h>

int main()
{
    int first[100], second[100];
    int i, size;

    /*
     * Reads size and elements in the array from user
     */
    printf("Enter the size of the array : ");
    scanf("%d", &size);
    printf("Enter elements of first array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &first[i]);
    }

    /*
     * Copies all elements from first array to second array
     */
    for(i=0; i<size; i++)
    {
        second[i] = first[i];
    }

    /* 
     * Prints all elements of first array
     */
    printf("\nElements of first array are : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", first[i]);
    }

    /*
     * Prints all elements of second array
     */
    printf("\nElements of second array are : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", second[i]);
    }

    return 0;
} 
Output
X
_
Enter the size of the array : 10
Enter elements of first array : 10 20 30 40 50 60 70 80 90 100

Elements of first array are : 10        20        30        40        50        60        70        80        90        100
Elements of second array are : 10        20        30        40        50        60        70        80        90        100

Happy coding ;)


You may also like

Labels: , ,