Swapping Values of Two Variables in C Language

Swapping the values of two variables is a fundamental operation in C language, and implementing it through functions is also a great example for understanding memory addresses and pointers.

Method 1: Using a Temporary Variable (Most Common Method)

This is the most straightforward and safest method, which involves introducing a third temporary variable as an intermediate storage.Swapping Values of Two Variables in C Language

Method 2: Without Using a Temporary Variable (Arithmetic Operations)

For numeric types, swapping can be achieved through arithmetic operations, but there is a risk of overflow.Swapping Values of Two Variables in C Language

Code Analysis:

  1. The principle is to use addition and subtraction to save the total of the two variables, and then restore the original values through subtraction.
  2. Advantages: No extra variable needed.
  3. Disadvantages:
  • Only applicable to numeric types.
  • If the result of <span>a + b</span> exceeds the maximum value of the variable type, it will cause overflow.
  • Poor readability.

Method 3: Using Pointers (Function Implementation)

In actual development, it is necessary to use functions to swap the values of variables, which requires the use of pointers.Swapping Values of Two Variables in C Language

Code Analysis:

  1. A <span>swap</span> function is defined, with parameters being two <span>int</span> type pointers (<span>int *</span>)
  2. Inside the function, the dereference operator <span>*</span> is used to access the variables pointed to by the pointers.
  3. When calling the <span>swap</span> function in the <span>main</span> function, the address operator <span>&</span> is used to get the addresses of the variables <span>a</span> and <span>b</span>.
  4. The key to this method is understanding:
  • Pointers store the memory addresses of variables.
  • The dereference operator <span>*</span> can access variables through their addresses.
  • Function parameters are passed by address, so the original variable values can be modified.

Why Can’t We Swap Directly Through Function Parameters?

Many beginners try the following code, but it cannot achieve swapping:

// Incorrect swap functionvoid wrong_swap(int x, int y) {    int temp = x;    x = y;    y = temp;}// After calling, the values of a and b will not changewrong_swap(a, b);

Reason: In C language, function parameters are passed by “value”, and the <span>x</span> and <span>y</span> inside the function are just copies of the original variables. Modifying the values of the copies will not affect the original variables, so this code cannot achieve swapping.

Summary

  1. Temporary Variable Method (Practical Development) is the most recommended method, intuitive, safe, and applicable to all data types.
  2. Arithmetic Method (Interviews) is suitable for simple numeric swaps but has overflow risks and poor readability.
  3. Pointer Function Method (Function) is used when swapping needs to be implemented through functions, and it is the standard way to handle such problems in C language.

Leave a Comment