C Language Daily Challenge: No.1 Swapping Values of Two Variables – How Many Methods Can You Write?

📌 Problem Description

Write a function to swap the values of two integer variables.

  • Example Input: a = 5, b = 10

  • Example Output: a = 10, b = 5

Difficulty: ⭐ (suitable for beginners, but how many methods can you think of?)

💡 Initial Thoughts

You might think: “Isn’t this just using a temporary variable to store the value?”

But, the real challenge is:

❓ If using a temporary variable is not allowed, can you still swap the values?

❓ What methods do you know that might be tested in exams?

📝 Method1: Temporary Variable Method (Most Basic)

void swap(int *a, int *b) {int temp = *a;  // Store the value of a in temp*a =*b;        // Assign the value of b to a*b= temp;      // Assign temp (original value of a) to b}

✔️ Advantages:

  • Clear logic, applicable to all data types (int, float, struct, etc.)

  • No overflow issues

Disadvantages:

  • Uses an extra variable, occupying an additional 4 bytes of memory (but usually negligible)

🔥 Method2: Arithmetic Method (No Temporary Variable)

void swap(int *a, int *b) {*a =*a + *b;  // a = a + b*b =*a - *b;  // b = (a + b) - b = a*a =*a - *b;  // a = (a + b) - a = b}

✔️ Advantages:

  • Saves one variable

  • Suitable for memory-sensitive scenarios

Disadvantages:

  • Possible overflow! If *a + *b exceeds INT_MAX, the result will be incorrect!

Method 3: Bitwise Method (High Frequency Exam Topic)

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

✔️ Advantages:

  • No overflow risk

  • Bitwise operations are efficient, suitable for embedded development

Disadvantages:

  • Poor readability, may be difficult for those unfamiliar with bitwise operations

C Language Daily Challenge: No.1 Swapping Values of Two Variables - How Many Methods Can You Write? In-depth Analysis

Method Applicable Scenarios Potential Issues
Temporary Variable General, Safe None
Arithmetic Operation Saves Memory Possible Overflow
Bitwise Operation High Performance Requirement Poor Readability

❓ Reflection:

  • Why do the formal parameters in a void function need to be pointers?

  • If the values being swapped are floating-point numbers, are these methods still applicable?

  • If the values being swapped are strings (char *), how would you write it?

🚀 Next Issue Preview

No.2: Using Macros (#define) to Swap Two Variables, Can You Do It?

📢 Interactive Time

1. Do you know other methods to swap variables? Feel free to leave a comment!

2. Which method do you think is the most clever? Vote:

  • 👍 Temporary Variable Method

  • ❤️ Arithmetic Method

  • 🔥 Bitwise Method

C Language Daily Challenge: No.1 Swapping Values of Two Variables - How Many Methods Can You Write?Friendly Reminder: This article only provides the core function implementation code. If you want to run it on a compiler, don’t forget to add the main function, and remember to pass parameters by address or pointer.

🚀 If you find this useful, feel free to share it with friends learning C Language!

Article Author: Vv Computer Graduate Exam World (Focusing on Computer Graduate Exam Tutoring for 8 years)

Original Statement: Please contact for authorization if reprinted, infringement will be pursued.

Leave a Comment