The Program in C to Swap Two Values using functions call by value is given below:
#include <stdio.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a, b;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter the value of a: 45
Enter the value of b: 56
Before swapping: a = 45, b = 56
After swapping: a = 45, b = 56
Pro-Tips💡
This program prompts the user to enter two values, ‘a’ and ‘b’, and then uses the ‘swap’ function to swap the values.
However, this program does not work as expected because the function ‘swap’ is using call by value method and in this method,
the function operates on a copy of the original variables, rather than the original variables themselves.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.