This is a C program to illustrate pass by value.
The program illustrates pass by value method.
1. Take two numbers as input.
2. Using a function, swap their values.
3. Print the output and exit.
Here is source code of the C Program to illustrate pass by value. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Illustrate Pass by Value.
void swap(int a, int b)
temp = a;
a = b;
b = temp;
int num1 = 10, num2 = 20;
printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
printf("After swapping num1 = %d num2 = %d \n", num2, num1);
1. Take two numbers as input and store it in the variables num1 and num2 respectively.
2. Call the function swap and pass the variables num1 and num2 as parameters to the function swap.
3. In function swap, recieve the parameters through variables a and b respectively.
4. Copy the value of variable a to the variable temp. Copy the value of variable b to the variable a and copy the value of variable temp to the variable b.
5. Print the variables num2 and num1 in the main function as output and exit.
Before swapping num1 = 10 num2 = 20 After swapping num1 = 20 num2 = 10
Sanfoundry Global Education & Learning Series – 1000 C Programs.