Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Replace Values Using Reference Loops

C Language Examples - Replace Values Using Reference Loops

C Language Examples C Language Examples

The three variables a, b, and c have their values cyclically replaced 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 swapping:

\

n

"

)

;

printf

(

"

a = %d

\

nb = %d

\

nc = %d

\

n

"

,

a

,

b

,

c

)

;

cyclicSwap

(

&

a

, &

b

, &

c

)

;

printf

(

"

After swapping:

\

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 is:



输入 a, b 和 c 的值: 1 2 3

Before swap:

a = 1 

b = 2 

c = 3

After swap:

a = 3 

b = 1 

c = 2

C Language Examples C Language Examples