c语言return的用法
时间: 2023-09-22 19:12:13 浏览: 51
return是C语言中的一个关键字,主要用于函数的返回值和程序的退出。
1. 函数的返回值:
函数可以有一个返回值,通常用于返回函数执行结果或错误码。函数的返回值类型必须在函数声明或定义中指定,可以是任何C数据类型,包括基本数据类型、结构体、指针等。
函数的返回值使用return语句返回,例如:
```c
int add(int a, int b)
{
return a + b;
}
```
2. 程序的退出:
在程序执行中,可以使用return语句退出程序。如果程序执行到了main函数中的return语句,则整个程序将退出。返回值表示程序的退出状态,通常返回0表示程序正常退出,其他值表示程序异常退出。
例如:
```c
int main()
{
printf("Hello, world!\n");
return 0;
}
```
上面的程序输出“Hello, world!”后,返回值为0,表示程序正常退出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)