Archive

Posts Tagged ‘operations’

Swapping variable values (examples in C)

October 10, 2013 No comments

So we want to make a variable named “a” have the value of a variable called “b” and vice versa. We can do it in several ways. (The examples may be silly but I will talk later about some good uses of it)

The first way is using a temporary variable so we (as original as we can), will call it tmp. We can do a program like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
  int a = 11;
  int b = 99;
  int tmp;

  printf ("Current a value: %d\n", a);
  printf ("Current b value: %d\n", b);
  printf ("\n\n"); // Some lines to make it clearer

  tmp = a; // so tmp=11; a=11; b=99;
  a = b;   // so tmp=11; a=99; b=99;
  b = tmp; // so tmp=11; a=99; b=11;

  printf ("New a value: %d\n", a);
  printf ("New b value: %d\n", b);

  return 0;
}

Compile it as you usually do with your IDE or in GCC with:

$ gcc -o swapvar swapvar.c

This type is swapping will be useful for any type of variable (int, float, char, structs, etc), but we must create a variable, that is, we will spend more memory (yeah, I know, memory is cheap nowadays, it would be a problem back in the 80′s, but not now).

We can create a function for this and the operation will be made by calling swap(a, b), just follow the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

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

int main()
{
  int a = 11;
  int b = 99;
  int tmp;

  printf ("Current a value: %d\n", a);
  printf ("Current b value: %d\n", b);
  printf ("\n\n"); // Some lines to make it clearer

  swap(&a, &b);

  printf ("New a value: %d\n", a);
  printf ("New b value: %d\n", b);

  return 0;
}

Don’t forget the asterisks, so we are passing arguments to swap function by reference (in other words, with pointers), when calling the function we use “ampersand variable” (&variable), getting the memory address of this variable and passing it to the function. Inside the function we use asterisks to know the value stored in the address the pointer is pointing to. It would be much more easy in C++, because to pass a variable by reference in C++ we don’t have to fight with pointers. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

void swap(int &a, int &b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

int main()
{
  int a = 11;
  int b = 99;
  int tmp;

  cout << "Current a value: " << a << endl;
  cout << "Current b value: " << b << endl;
  cout << endl << endl;

  swap(a, b);

  cout << "New a value: " << a << endl;
  cout << "New b value: " << b << endl;

  return 0;
}

The other methods are only applicable when switching numeric variables (char, short, int, unsigned, long), because we will do mathematical operations with variables (float and double won’t always work due to precision problems. But I post them, for curious people, but in real life I’ve always used the first method.

Addition and subtraction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
  int a = 11;
  int b = 99;
  // No temporary var :)

  printf ("Current a value: %d\n", a);
  printf ("Current b value: %d\n", b);
  printf ("\n\n"); // Some lines to make it clearer

  a = a + b;  // a = 110 ; b = 99;
  b = a - b;  // a = 110 ; b = 11
  a = a - b;  // a = 99 ; b = 11

  printf ("New a value: %d\n", a);
  printf ("New b value: %d\n", b);

  return 0;
}

The other example is using the XOR operation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
  int a = 11;
  int b = 99;
  int tmp;

  printf ("Current a value: %d\n", a);
  printf ("Current b value: %d\n", b);
  printf ("\n\n"); // Some lines to make it clearer

  a ^= b;  // a = 104; b = 111          /* a = a xor b */
  b ^= a;  // a = 104; b = 11           /* b = b xor a */
  a ^= b;  // a = 99 ; b = 11           /* a = a xor b */

  printf ("New a value: %d\n", a);
  printf ("New b value: %d\n", b);

  return 0;
}

XOR is made bit by bit, it’s a binary opeator. R = A XOR B. R will result 1 only if one of the operators (A and B) is 1 (the other must be zero). So R will be 1 if A=1 and B=0 or A=0 and B=1. Making the operation in a numeric value means we will use the binary value of each number so:

11 = 00001011
99 = 01100011
————————
11^99 = 01101000 = 104

104 = 01101000
99 = 01100011
————————
104^99 = 00001011 = 11

104 = 01101000
11 = 00001011
——————–
104^11 = 01100011 = 99

That’s it, you can find this post in Spanish (not exactly the same content) in http://totaki.com/poesiabinaria/2009/06/intercambiar-valores/

Photo: I’ll never give up (Flickr) CC-by

Top