怎样将int main函数改为void fun
时间: 2023-12-31 07:03:57 浏览: 36
将原来的 `int main()` 改为 `void fun()`,并且去掉 `return` 语句即可。例如:
```c++
#include <iostream>
void fun() {
std::cout << "Hello, world!" << std::endl;
}
int main() {
fun();
return 0;
}
```
改为:
```c++
#include <iostream>
void fun() {
std::cout << "Hello, world!" << std::endl;
}
int main() {
fun();
}
```
注意,`main` 函数是程序的入口,它的返回值表示程序的结束状态,一般来说返回值为 0 表示程序正常结束,非 0 表示程序异常结束。因此在将 `main` 函数改为其他函数时,需要注意函数的返回值是否符合要求。在上面的例子中,我们去掉了 `return` 语句,表示程序正常结束。
相关问题
编写一个函数fun,交换两个数的值,要求使用指针实现。然后再main函数中调用fun函数。fun函数的头部为void fun (int *a,int *b),用C语言
以下是使用指针交换两个数的值的C语言代码:
```c
#include <stdio.h>
void fun(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swapping: x = %d, y = %d\n", x, y);
fun(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
```
输出:
```
Before swapping: x = 10, y = 20
After swapping: x = 20, y = 10
```
编写函数void fun(int arr[],int n)将一个数组中的值按逆序存放,并在main()函数中输出
以下是实现代码:
```c
#include <stdio.h>
void fun(int arr[], int n) {
int i, temp;
for (i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Before reverse: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
fun(arr, n);
printf("\nAfter reverse: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
函数`fun`将数组中的元素按逆序存放,使用了双指针法,遍历数组前一半,将前后两个元素互换位置。在`main`函数中,我们首先输出原始数组,再调用`fun`函数进行逆序,最后输出处理后的数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)