YouTip LogoYouTip

C Examples Swapping Cyclic Order

## C Program to Swap Numbers in Cyclic Order Using Call by Reference In C programming, swapping two variables is a fundamental exercise. However, swapping three or more variables in a **cyclic order** requires a slightly different approach. This tutorial demonstrates how to perform a cyclic swap of three variables ($a$, $b$, and $c$) using pointers (call by reference) in C. --- ### Understanding Cyclic Swapping A cyclic swap shifts the values of variables in a circular loop. For three variables $a$, $b$, and $c$, a cyclic swap shifts the values as follows: * The value of $b$ moves to $a$ (or vice versa, depending on the direction). * The value of $c$ moves to $b$. * The value of $a$ moves to $c$. In this specific implementation, we will perform the following shift: $$\text{New } a = c, \quad \text{New } b = a, \quad \text{New } c = b$$ To modify the actual values of the variables declared in the `main()` function from within a custom swap function, we must pass them **by reference** using pointers. --- ### Complete C Code Example Below is the complete, compilable C program that implements cyclic swapping using pointers. ```c #include // Function prototype void cyclicSwap(int *a, int *b, int *c); int main() { int a, b, c; // Prompt user for input printf("Enter the values of a, b, and c: "); scanf("%d %d %d", &a, &b, &c); printf("\n--- Before Swapping ---\n"); printf("a = %d\nb = %d\nc = %d\n", a, b, c); // Pass the addresses of the variables (Call by Reference) cyclicSwap(&a, &b, &c); printf("\n--- After Swapping ---\n"); printf("a = %d\nb = %d\nc = %d\n", a, b, c); return 0; } /** * Performs a cyclic swap of three integers. * The values are shifted: b -> temp, a -> b, c -> a, temp -> c */ void cyclicSwap(int *a, int *b, int *c) { int temp; // Store the value of b in a temporary variable temp = *b; // Copy the value of a into b *b = *a; // Copy the value of c into a *a = *c; // Copy the stored value of b (from temp) into c *c = temp; } ``` --- ### Sample Output ```text Enter the values of a, b, and c: 1 2 3 --- Before Swapping --- a = 1 b = 2 c = 3 --- After Swapping --- a = 3 b = 1 c = 2 ``` --- ### Code Explanation #### 1. Call by Reference In C, functions are "call by value" by default, meaning they receive copies of arguments. To modify the original variables, we pass their memory addresses using the address-of operator (`&a`, `&b`, `&c`). The function parameters are declared as pointers (`int *a`, `int *b`, `int *c`) to accept these addresses. #### 2. Dereferencing Operator (`*`) Inside the `cyclicSwap` function, we use the dereferencing operator (`*`) to access and modify the actual values stored at those memory addresses. #### 3. The Swap Logic To prevent values from being overwritten and lost during the swap, we use a single temporary variable `temp`: 1. `temp = *b;` β€” Saves the original value of $b$. 2. `*b = *a;` β€” Overwrites $b$ with the value of $a$. 3. `*a = *c;` β€” Overwrites $a$ with the value of $c$. 4. `*c = temp;` β€” Overwrites $c$ with the saved original value of $b$. --- ### Key Considerations * **Pointer Safety:** Always ensure that valid memory addresses are passed to the pointer parameters to avoid segmentation faults. * **Generics:** This example swaps `int` types. If you need to swap `float`, `double`, or custom `struct` types, you must change the function signature and the temporary variable type accordingly, or use a generic `void *` pointer approach with memory copying (`memcpy`).
← C Examples Remove Characters SC Examples Standard Deviation β†’