C语言实现找20,19,31,59最小数,用指针和不用指针两种方法
时间: 2023-07-15 08:12:14 浏览: 19
使用指针方法实现:
```c
#include <stdio.h>
int main() {
int a = 20, b = 19, c = 31, d = 59;
int *p = &a, *q = &b, *r = &c, *s = &d;
int min = *p;
if (min > *q) {
min = *q;
}
if (min > *r) {
min = *r;
}
if (min > *s) {
min = *s;
}
printf("最小值为:%d", min);
return 0;
}
```
不使用指针方法实现:
```c
#include <stdio.h>
int main() {
int a = 20, b = 19, c = 31, d = 59;
int min = a;
if (min > b) {
min = b;
}
if (min > c) {
min = c;
}
if (min > d) {
min = d;
}
printf("最小值为:%d", min);
return 0;
}
```
相关问题
用c语言输出指针指向的小数,并保留两位小数
可以使用printf函数来输出指针指向的小数,并使用格式化字符串指定输出的小数位数。例如:
```c
#include <stdio.h>
int main() {
double x = 3.14159;
double *p = &x;
printf("%.2f\n", *p);
return 0;
}
```
输出:
```
3.14
```
在格式化字符串中,%.2f表示输出一个小数,并保留两位小数。*p表示输出指针p指向的值,即x的值。因此,上述代码输出的是3.14。
不用库函数,用c语言分离小数的整数部分和小数部分
以下是用C语言将小数分离为整数部分和小数部分的示例代码:
```c
#include <stdio.h>
void separateDecimal(float num, int *integerPart, float *decimalPart) {
*integerPart = (int)num;
*decimalPart = num - *integerPart;
}
int main() {
float number = 3.14159;
int integerPart;
float decimalPart;
separateDecimal(number, &integerPart, &decimalPart);
printf("Integer Part: %d\n", integerPart);
printf("Decimal Part: %f\n", decimalPart);
return 0;
}
```
这段代码将浮点数 `3.14159` 分离为整数部分 `3` 和小数部分 `0.141590`。函数 `separateDecimal` 接受一个浮点数作为参数,并使用指针将整数部分和小数部分的结果返回给调用者。在 `main` 函数中,我们将要分离的浮点数传递给 `separateDecimal` 函数,并打印出结果。
请注意,此示例假设浮点数为正数。如果需要处理负数情况,请根据实际需求进行修改。
相关推荐
![](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)