All articles
c

A program to explain CALL BY REFERENCE (address)

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 083

  • Write a program to explain CALL BY REFERENCE (address)

Program

#include <stdio.h>
 
void swap(int *, int *);
void main()
{
    int a = 10, b = 20;
 
    printf("Before swapping...\n");
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("\n");
 
    swap(&a, &b);
    printf("After swapping...\n");
    printf("a = %d\n", a);
    printf("b = %d\n", b);
 
    printf("\n");
}
 
void swap(int *x, int *y)
{
    int t;
    t = *x;
    *x = *y;
    *y = t;
}

Result

Before swapping...
a = 10
b = 20

After swapping...
a = 20
b = 10

Comments