main函数返回值?

athirs 2006-09-15 10:17:30
请教各位高手,
main函数的返回值有什么用呢?
在vc里写return 0;和return -1;似乎没有什么区别阿
...全文
507 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
athirs 2006-09-15
  • 打赏
  • 举报
回复
thanks a lot to all of you! :)
hailongchang 2006-09-15
  • 打赏
  • 举报
回复
下面是一个国外新闻组上的讨论,我认为讲的非常清楚,lz可以看看

Typically the return value is used as a exit status code (I prefer to use the exit () function myself). So that, if your program is called by another program or a shell/batch script, the calling entity can react to the exit status appropriately.

To explain this a little more clearly, assume your program parses a large file and inserts the parsed data into a database. Your program is called by a user clicking a button in some other program. How does the user know if your program successfully completed the task? Or, if it was not completed successfully, why?

The easiest way to tell what happened after your program has completed is to check the returned exit status code. Most of the time these codes are defined by you. For instance, 0 if it completed okay, 1 if it could not connect to the database, 2 if it deadlocked with another user in the database, 3 if it couldn't find the file to parse, etc. Then, using these codes, the calling program can print the appropriate message to the user's screen, or use some sort of logic that will attempt to get your program to complete successfully.
Jokar 2006-09-15
  • 打赏
  • 举报
回复
同上~
lj860603 2006-09-15
  • 打赏
  • 举报
回复
大多数C语言实现都是通过函数main的返回值来告知操作系统函数的执行是否成功。
返回值为0代表程序执行成功,返回值非0则表示程序执行失败。
mu_yang 2006-09-15
  • 打赏
  • 举报
回复
这是给操作系统的信息

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧