exit() 与 return ()

return是返回,也就是说返回一个值给函数
其他函数就可以收到这个函数的返回值

而exit是不一样的,exit表示直接退出程序
该进程直接结束
exit(0)表示正常退出
exit(其他整数)表示异常退出

其他函数是不可能收到任何的信息的,因为进程已经结束。

附上代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
	printf("%d\n",f1());
	return 0;
}

int f1()
{
	int i;
	i=10;
	return i;
}

这样的话函数是有返回值的,返回了i的值
(如果不写return i; 会默认返回1)

#include <stdio.h>
#include <stdlib.h>

int main()
{
	printf("%d\n",f1());
	return 0;
}

int f1()
{
	int i;
	i=10;
	exit(0);
}

但是这种情况就不一样了,这样进程是直接被杀死的
根本就不会有返回值
在主函数里面自然就打印不出来任何东西了
整个程序运行到exit这里就结束了

猜你喜欢

转载自blog.csdn.net/qq_39054069/article/details/84202757
今日推荐