Chinaunix首页 | 论坛 | 博客
  • 博客访问: 122201
  • 博文数量: 23
  • 博客积分: 1522
  • 博客等级: 上尉
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-28 09:55
文章分类

全部博文(23)

文章存档

2011年(6)

2009年(2)

2008年(15)

分类: LINUX

2009-10-12 09:40:17

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语句。
阅读(770) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~