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

C Exercise 66

C Exercise 66

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Enter three numbers a, b, c and output them in order of magnitude.

**Program Analysis:** Use the pointer method.

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. All rights reserved.

//

include

<

stdio

.

h

void

swap

(

int

*,

int

)

;

int

main

(

void

)

{

int

a

,

b

,

c

;

int

p1

, *

p2

, *

p3

;

printf

(

"

Enter a, b, c:

\

n

"

)

;

scanf

(

"

%d %d %d

"

, &

a

, &

b

, &

c

)

;

p1

= &

a

;

p2

= &

b

;

p3

= &

c

;

if

(

a

b

)

swap

(

p1

,

p2

)

;

if

(

a

c

)

swap

(

p1

,

p3

)

;

if

(

b

c

)

swap

(

p2

,

p3

)

;

printf

(

"

%d %d %d

\

n

"

,

a

,

b

,

c

)

;

}

void

swap

(

int

s1

,

int

s2

)

{

int

t

;

t

= *

s1

; *

s1

= *

s2

; *

s2

=

t

;

}

The output of the above program is:



输入 a, b ,c:

1 3 2

1 2 3

100 Classic C Language Examples 100 Classic C Language Examples