用C语言指针给你三个整数,请按从小到大的顺序输出。
时间: 2024-02-16 17:03:26 浏览: 34
好的,下面是用C语言指针实现的按从小到大排序的代码:
```c
#include <stdio.h>
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;
}
}
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("从小到大排序后的结果为:%d %d %d\n", a, b, c);
return 0;
}
```
运行结果:
```
请输入三个整数:
7 3 9
从小到大排序后的结果为:3 7 9
```
希望能帮到你,如果还有其他问题,可以继续问我哦!
相关问题
用c语言编写,输入三个整数,用指针的方法从小到大输出
可以这样编写:
```c
#include <stdio.h>
void sort(int *a, int *b, int *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;
}
void sort(int *a, int *b, int *c) {
if (*a > *b) {
int temp = *a;
*a = *b;
*b = temp;
}
if (*a > *c) {
int temp = *a;
*a = *c;
*c = temp;
}
if (*b > *c) {
int temp = *b;
*b = *c;
*c = temp;
}
}
```
这个程序中,我们定义了一个 `sort` 函数,它接受三个指向整数的指针,用来对三个整数进行排序。在 `main` 函数中,首先读入三个整数,然后调用 `sort` 函数进行排序,最后输出排序后的结果。
用C语言指针针写一个从键盘随机输入三个整数按由小到大的顺序输出
好的,以下是用C语言指针实现的代码:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c)
{
if (*a > *b) {
int temp = *a;
*a = *b;
*b = temp;
}
if (*a > *c) {
int temp = *a;
*a = *c;
*c = temp;
}
if (*b > *c) {
int temp = *b;
*b = *c;
*c = temp;
}
}
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;
}
```
这段代码首先定义了一个 `sort` 函数,它接受三个整数指针作为参数,并将它们按由小到大排序。然后在主函数中,我们读取三个整数,调用 `sort` 函数进行排序,最后输出排序后的结果。
需要注意的是,在调用 `sort` 函数时,我们需要传递三个整数的地址,即 `&a`, `&b`, `&c`,这样 `sort` 函数才能修改它们的值。在函数内部,我们使用 `*a`、`*b`、`*c` 来访问这三个整数的值。
你可以将这段代码复制到你的编辑器中,并编译运行它。它会读取三个整数,并按由小到大的顺序输出它们。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)