Swapping Variables

Difficulty: ★ ★ ★ ☆

The classic paradigm for swapping values between two variables involves using a third variable. But is the third variable even necessary?

For example, to swap a and b, you use variable c:

c = a;
a = b;
b = c;

At the end of the operation, the values of variables a and b are swapped, with variable c left with nothing else to do. But is variable c even necessary?

It turns out you need only two variables to swap their values — odd as it sounds. The process involves a bit of math and some head scratching, but it works. Rather than rush to StackOverflow for the answer, try on your own to think of a way to swap two variables’ values, which is the challenge for this month’s Exercise.

Here’s a skeleton to get you started:

2023_06_01-Lesson.c

#include <stdio.h>

int main()
{
    int a,b;

    printf("Enter value A: ");
    scanf("%d",&a);
    printf("Enter value B: ");
    scanf("%d",&b);

    printf("Before: A=%d, B=%d\n",a,b);

    /* swap variables */

    printf("After: A=%d, B=%d\n",a,b);

    return(0);
}

As the code stands now, two values are fetched from standard input. Both are output, then comes the swap (the part you write) and the results are output:

Enter value A: 10
Enter value B: 45
Before: A=10, B=45
After: A=10, B=45

Your challenge is to add code after the comment, fancy math stuff that swaps the values without adding a third variable. In fact, as a hint, you need three lines of code (three statements) just as if you were using a third variable.

Here is the output from my solution:

Enter value A: 10
Enter value B: 45
Before: A=10, B=45
After: A=45, B=10

Please O please do try this exercise on your own. The solution is widely available on the interwebs, but I had a good time trying to make the swap happen without cheating. You can view my solution here.

3 thoughts on “Swapping Variables

  1. You don’t need 3 lines of code, only 1:

    printf(“After: A=%d, B=%d\n”,b,a);

    🙂 🙂 🙂

    Seriously, I don’t think this is a 3 star problem, it’s more of a 30 or 300 star problem. When I first saw the solution you are getting at years ago I thought it must have been invented by some super intelligent megabeing from the planet Zarg. Or maybe I’m just too stupid to invent stuff like that.

    btw I think it only works with integers or integer-like types, ie. C chars. I don’t think it works with floats etc.

  2. After a bit of messing about I’ve managed to get the 3 lines of code down to one. This is the sort of things C programmers used to do in prehistoric times to show how clever they were, but they only made themselves look stupid by thinking this sort of thing was a good idea. Here it is:

    #include<stdio.h>
    #include<stdlib.h>

    int main()
    {
        int a = 45;
        int b = 99;

        printf(“Before: a = %d, b = %d\n”, a, b);

        a = a^(b=(a = a^b)^b);

        printf(“After: a = %d, b = %d\n”, a, b);

        return EXIT_SUCCESS;
    }

    I’m not 100% certain it’s flawless but it works with 45 and 99 which is good enough for me. If anyone can spot values which will break the code please let me know.

Leave a Reply