Pages

Saturday, November 08, 2014

Write a program that swap two integer by function call

Input:

//Write a program that swap two integer by function call (passing by reference/address)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>

void swap(int *x, int *y)
{

    int temp;
    printf("x = %d, y = %d \n", x, y);
    temp = *x;
    x = *y;
    y = temp;
    printf("x = %d, y = %d\n\n", x, y);
}
void main()
{
    int x = 5;
    int y = 10;
    swap(x,y); //passing by reference/address
}

Output:

x = 5, y = 10
x = 10, y = 5

No comments:

Post a Comment