C Language Example - Replacing Values Using Reference Loop

C Language Examples

The three variables a, b, c, cyclically replace their values in order by reference.

Example

#include<stdio.h> void cyclicSwap(int *a,int *b,int *c); int main() { int a, b, c; printf("Enter the values of a, b and c:"); scanf("%d %d %d",&a,&b,&c); printf("Before swap:\n"); printf("a = %d \nb = %d \nc = %d\n",a,b,c); cyclicSwap(&a, &b, &c); printf("After swap:\n"); printf("a = %d \nb = %d \nc = %d",a, b, c); return 0; } void cyclicSwap(int *a,int *b,int *c) { int temp; // Swap temp = *b; *b = *a; *a = *c; *c = temp; }

The output result is:

输入 a, b 和 c 的值: 1 2 3
交换前:
a = 1 
b = 2 
c = 3
交换后:
a = 3 
b = 1 
c = 2

C Language Examples

other extensions