BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Swap two numbers using Pointers

Last Updated: February 24, 2019 by Chaitanya Singh | Filed Under: C Programs

In this tutorial we will write a C program to swap two numbers using Pointers. We have already covered how to swap two numbers without using pointers.

C Example to swap two numbers using pointers

/*C program by Chaitanya for beginnersbook.com
 * Program to swap two numbers using pointers*/
#include <stdio.h>

// function to swap the two numbers
void swap(int *x,int *y)
{
    int t;
     t   = *x;
    *x   = *y;
    *y   =  t;
}

int main()
{
    int num1,num2;

    printf("Enter value of num1: ");
    scanf("%d",&num1);
    printf("Enter value of num2: ");
    scanf("%d",&num2);

    //displaying numbers before swapping
    printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

    //calling the user defined function swap()
    swap(&num1,&num2);

    //displaying numbers after swapping
    printf("After  Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

    return 0;
}

Output:
C Program to swap two numbers using pointers

Related C Examples

1. C program to declare, initialize and access a pointer
2. C program to check whether a char is an alphabet or not
3. C program to convert decimal to Octal
4. C program to find Quotient and Remainder

Top Related Articles:

  1. C Program to find the Size of int, float, double and char
  2. C Program to calculate Area of an Equilateral triangle
  3. C Program to Find GCD of two Numbers
  4. C Program to concatenate two strings without using strcat
  5. C Program to Generate Multiplication Table

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap