This is because you pass variables by copy and not by pointer. In other words, your swap()
functions receives its own private copies of x
and y
and swap them and the result of swap is not visible by the caller. The correct code might look something like this:
#include
void swap(int *a, int *b);
int main(void)
{
int x = 1;
int y = 2;
swap(&x, &y);
printf("Now x is %d and y is %d\n", x, y);
return 0;
}
//function definition of swap
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}