variable swap in c
Variable Swap in C
To swap two variables in C, you can use a temporary variable. Here are the steps:
- Declare the Variables: Declare the two variables that you want to swap.
- Initialize the Variables: Assign values to the variables.
- Create a Temporary Variable: Create a temporary variable to hold the value of one of the variables.
- Swap the Variables: Use the temporary variable to swap the values of the two variables.
Here's an example of how this can be done in C:
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
printf("Before swapping: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
[[SOURCE #2]]