C 语言实例 - 使用引用循环替换数值

C 语言教程 · 2019-02-21 08:13:03

a、b、c 三个变量,通过引用按顺序循环替换他们的值。

实例

#include<stdio.h> void cyclicSwap(int *a,int *b,int *c); int main() { int a, b, c; printf("输入 a, b 和 c 的值: "); scanf("%d %d %d",&a,&b,&c); printf("交换前:\n"); printf("a = %d \nb = %d \nc = %d\n",a,b,c); cyclicSwap(&a, &b, &c); printf("交换后:\n"); printf("a = %d \nb = %d \nc = %d",a, b, c); return 0; } void cyclicSwap(int *a,int *b,int *c) { int temp; // 交换 temp = *b; *b = *a; *a = *c; *c = temp; }

输出结果为:

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

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

A=B

A=B

Marko Petkovsek、Herbert S. Wilf、Doron Zeilberger / AK Peters, Ltd. / 1996-01 / USD 49.00

At some point, this book describes methods of solving the problem raised by Donald E. Knuth in the classical book "The Art of Computer Programming, Volume 1: Fundamental Algorithms". The main purpo......一起来看看 《A=B》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具