用c语言顺序输出 输入3个整数,利用指针和函数相关知识将数字按由小到大顺序输出 输入说明: 3个整数 输入说明: 由小到大顺序输出 输入示例: 1 2 3 输出示例: 1 2 3
时间: 2023-10-31 10:21:56 浏览: 30
好的,下面是实现代码:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c) { // 定义排序函数
int tmp;
if (*a > *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a > *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b > *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
sort(&a, &b, &c); // 调用排序函数,传入三个变量的地址
printf("%d %d %d", a, b, c); // 按顺序输出三个变量的值
return 0;
}
```
输入三个整数后,将它们的地址传入排序函数中,排序函数按由小到大的顺序对这三个变量进行排序,然后在 `main` 函数中输出这三个变量的值即可。
相关问题
C语言用指针实现:输入3个整数,按由大到小的顺序输出
好的,这是一个比较基础的 C 语言问题,可以使用指针实现,具体代码如下:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *a, int *b, int *c) {
if (*a < *b) swap(a, b);
if (*a < *c) swap(a, c);
if (*b < *c) swap(b, c);
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("%d %d %d\n", a, b, c);
return 0;
}
```
在这个程序中,我们首先定义了一个 `swap` 函数,用于交换两个整数的值,它接受两个指向整数类型变量的指针作为参数。然后定义了一个 `sort` 函数,用于对输入的三个整数按由大到小的顺序进行排序,它接受三个指向整数类型变量的指针作为参数。
在 `main` 函数中,我们首先从标准输入读取三个整数,并将它们分别赋值给变量 `a`、`b` 和 `c`。然后调用 `sort` 函数,对它们进行排序。最后按照从大到小的顺序输出它们的值。
C语言编程用指针变量作为函数参数,实现:输入3个整数,按由大到小的顺序输出
好的,可以参考以下的C代码实现。
```
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main() {
int x, y, z;
printf("请输入三个整数:");
scanf("%d %d %d", &x, &y, &z);
sort(&x, &y, &z);
printf("由大到小排序后的结果为:%d %d %d", x, y, z);
return 0;
}
void sort(int *a, int *b, int *c) {
int temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
```
这里定义了一个名为`sort`的函数,接收三个参数,分别为整型指针类型的`a`、`b`和`c`。在函数中,通过比较指针所指向的值的大小关系,实现了由大到小的排序。同时,由于传入的是指针变量,函数内部对变量的修改也会影响到外部变量的值。在主函数中,通过调用`sort`函数,实现了输入三个整数,并按要求输出的功能。
阅读全文
相关推荐












