I try to swap values of two variables in C, I wrote function but it doesnt work and I cant figure it out whats wrong.
#include <stdio.h>
void fun(int a_local, int b_local)
{
int temp = a_local;
a_local = b_local;
b_local = temp;
}
int main()
{
int a_global = 5;
int b_global = 7;
printf("a=%d, b=%d\n", a_global, b_global);
fun(a_global, b_global);
printf("a=%d, b=%d\n", a_global, b_global);
return 0;
}
a_localandb_localhave the opposite value that they started life with. But that swap does not affect their parents:a_global,b_global.