Swap two variables showing the example of call by reference - C

Q. Write a program to swap two variables showing the example of call by reference. 
#BIT_FirstSem_2017_6No_4Marks_Solution
==>Code goes like this:

#include<stdio.h>
void swap(int *, int *);
int main()
{
    int a,b;
    printf("Enter two numbers:\n");
    scanf("%d %d",&a,&b);
    printf("Before Swapping: %d %d\n",a,b);
    swap(&a,&b);
    printf("\nAfter Swapping: %d %d",a,b);
    return 0;
}

void swap(int *a, int *b)
{
    int c;
    c=*a;
    *a=*b;
    *b=c;
}   

Output:







If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment