Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1145968
  • 博文数量: 254
  • 博客积分: 1242
  • 博客等级: 少尉
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-03 21:49
文章分类

全部博文(254)

文章存档

2017年(16)

2016年(4)

2013年(94)

2012年(140)

分类:

2012-07-16 09:33:33

原文地址:return 与 exit 作者:abncat

return是指从函数中返回,如果是子函数,就是从子函数返回到调用函数中。如果过是主函数则推出程序。

exit是从程序中退出,无论是出现在主程序还是子程序中,当调用exit(1)时,程序即退出。


#include <stdlib.h>
#include <stdio.h>
void fn1()
{
        printf("here is fn1\n");
        exit(1);
}
void fn2()
{
        printf("here is fn2\n");
        return ;
}
int
main(int argc, char ** argv)
{
        printf("here is main\n");
        printf("at the begin of the function to call\n");
        fn2();
        printf("at the end of the function to call\n");
        return 0;


}

如图函数当调用函数fn2时结果如图:

[root@bogon wangxw]# ./test
here is main
at the begin of the function to call
here is fn2
at the end of the function to call

把main函数中的fn2替换为fn1时,运行结果如图:

[root@bogon wangxw]# ./test
here is main
at the begin of the function to call
here is fn1
[root@bogon wangxw]#

可见调用exit时函数就直接从调用点fn1处退出,没有打印main语句的printf的语句;调用return是从fn2返回到main函数中执行了后面的printf语句。
阅读(496) | 评论(0) | 转发(0) |
0

上一篇:grep

下一篇:return与exit

给主人留下些什么吧!~~