forked from avinashbest/codewithharry-c-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37_call_by_reference.c
More file actions
33 lines (28 loc) · 764 Bytes
/
37_call_by_reference.c
File metadata and controls
33 lines (28 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
void swap(int *x, int *y);
void wrong_swap(int x, int y);
int main(void)
{
int a = 3, b = 5;
printf("\nThe value of a and b before swapping is %d and %d.\n", a, b);
wrong_swap(a, b); //will not work bcoz of call by value.
printf("The value of a and b after swapping is %d and %d.\n\n", a, b);
printf("\nThe value of a and b before swapping is %d and %d.\n", a, b);
swap(&a, &b); //will work due to call by reference
printf("The value of a and b after swapping is %d and %d.\n\n", a, b);
return 0;
}
void wrong_swap(int x, int y) //call by value
{
int temp;
temp = x;
x = y;
y = temp;
}
void swap(int *x, int *y) //call by reference
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}