main函数返回值作用

转贴:http://topic.csdn.net/t/20060915/10/5023777.html

(hailongchang)下面是一个国外新闻组上的讨论,我认为讲的非常清楚,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.

没有更多推荐了,返回首页