---
title: "Swap two values using pointers"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-swap-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-swap-function
site: BCT Engineering
---

# Swap two values using pointers

Computer Programming · Chapter 9: Pointers

- Asked in: 2071 Shrawan, 6 marks
- Video: [Swap Two Variables Using Pointers | C Programming Example](https://www.youtube.com/watch?v=ow5BZ0pZaUY) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-swap-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

Write a program using pointer to swap the value of two variables where the swapping operation is performed in separate function.

## Short answer

To swap two variables via a function, pass their addresses (call by reference). The function receives two pointers, dereferences them to exchange values using a temporary variable. Main calls swap(&x, &y); prints before (e.g., 15, 40) and after (40, 15).

## Answer

To change the original variables inside a function, their addresses are passed (call by reference). The function receives two pointers and exchanges the values stored at those addresses using a temporary variable.

```c
#include <stdio.h>

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

int main(void)
{
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    printf("Before swap: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swap : x = %d, y = %d\n", x, y);
    return 0;
}
```

**Sample run**

Input: `15 40`

```text
Enter two numbers: Before swap: x = 15, y = 40
After swap : x = 40, y = 15
```

If the values were passed instead of the addresses, only copies would be swapped and `x` and `y` would stay unchanged in `main()`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why does swap(&x, &y) change x and y in main()?
   A. The function receives their addresses and works on the originals
   B. The function returns both values
   C. x and y are global
   D. C passes arrays by value
   Answer: A. With addresses the function can modify the original variables through the pointers. (Key point: Why pass addresses)

2. Which sequence correctly swaps *a and *b?
   A. t = *a; *a = *b; *b = t;
   B. *a = *b; *b = *a;
   C. t = a; a = b; b = t;
   D. *a = t; t = *b; *b = *a;
   Answer: A. The old *a is saved in t, *a takes *b, then *b takes the saved value. (Key point: Swap body)

3. What would happen if the values were passed instead of the addresses?
   A. x and y would still be swapped
   B. Only copies would be swapped and x and y stay unchanged
   C. The program would not compile
   D. x and y would both become 0
   Answer: B. The function would swap its own local copies, leaving main()'s variables unchanged. (Key point: Pass by value)

4. With input 15 and 40, what is printed after the swap?
   A. x = 15, y = 40
   B. x = 40, y = 15
   C. x = 40, y = 40
   D. x = 15, y = 15
   Answer: B. The two values are exchanged. (Key point: Result)

## Related in Computer Programming

- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×
- [Sum and average between M and N using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-sum-average-range) — Computer Programming, chapter 9
- [Why datatype in pointer declaration](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-datatype-needed) — Computer Programming, chapter 9
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6

## Related in other subjects

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Extreme values with two constraints](https://www.bctengineer.com/question.md?key=semester_2%2Fmathematics%3Aextreme-values-two-constraints) — Mathematics, chapter 1, asked 2×
- [Per unit values: 100 MVA generator, two motors](https://www.bctengineer.com/question.md?key=semester_4%2Fpower_system_analysis_i%3Aper-unit-100mva-6kv-two-motors) — Power System Analysis I, chapter 3
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
