DUMMY_CHAPTER_NAME

Write a C program to swap (interchange) the two given variable (Using Pointer and Call by Value Function)

Call by Value - Using Pointers and Functions - C Programming

Write a C program to swap (interchange) the two given variable.

/* C Program to swap the two given variable *

#include <stdio.h>

main()

{

int a=10, b=20;   /* Local definitions */

printf ("\n. Before swap a=%d,b=%d",a,b);

void swap(a,b); /* swap is a function with parameters */

printf ("\n After the calling the swap function, a= %d,b=%d", swap(a,b);

} /* main */

void swap(x,y)

int x,y;

{

x=x+y;

y=x‒y;

x=x‒y;

printf ("In swap function x= %d,y=%d",x,y);

} /* swap */

OUTPUT

Before swap, a=10, b=20.

In swap function x=20, y=10.

After calling the swap function a=10, b=20.

EXPLANATION: The main function control transfer to the user defined function swap(). Here the variable a,b are referred as x,y. Inside the function block, the values are exchanged one to another without using third variable, then print the values. The main program prints the values of variable a and b after called the function.

DUMMY_CHAPTER_NAME : Tag: : - Write a C program to swap (interchange) the two given variable (Using Pointer and Call by Value Function)


DUMMY_CHAPTER_NAME



Under Subject



Related Subjects