Swap Values in C

Level 379 keystrokes

Swaps the values of a and b through a temporary variable temp, then prints "a=10, b=5".

C code

// Declares the two variables to swap: a is 5, b is 10
1int a = 5, b = 10;
// Saves a (5) first, because the next line overwrites it
2int temp = a;
// a takes the value of b and becomes 10
3a = b;
// b gets the saved old value of a, 5
4b = temp;
// Prints a=10, b=5, showing that the values were swapped
5printf("a=%d, b=%d\n", a, b);

Output

a=10, b=5
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • =× 7
  • ;× 5
  • ,× 4
  • "× 2
  • %× 2
  • (× 1
  • )× 1
  • \× 1

More C examples at level 3

← C typing practice