从main()返回错误 [英] returning error from main()
问题描述
伙计们,
如果main()调用了一些函数func()并且该函数返回
错误(错误号),那么它是否有意义从main返回该值
(errno)。 (如果主要不能继续进行)?
例如。
int main(无效)
{
int error;
if((error = func())0){
return(error); /* 这个可以吗。 ? * /
}
返回0;
}
否则,如果这不是正确的事情,还应该做些什么?
Guys,
If main() calls some function func() and that function returns
the error (errno), then does it make sense to return that value
(errno) from main. (in case main can''t proceed further) ?
eg.
int main(void)
{
int error;
if ((error = func()) 0) {
return(error); /* Is this Ok. ? */
}
return 0;
}
Otherwise, if this is not the right thing, what else should be done ?
推荐答案
" ju ********* *@yahoo.co.in" < ju ********** @ yahoo.co.inwrites:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
int main(void)
{
int error;
if((error = func())0){
return(error); /* 这个可以吗。 ? * /
}
返回0;
}
int main(void)
{
int error;
if ((error = func()) 0) {
return(error); /* Is this Ok. ? */
}
return 0;
}
这似乎是一个坏主意,至少有三个原因。首先,C
只定义了main的三个返回值的含义(
wit,0,EXIT_FAILURE和EXIT_SUCCESS)。其次,许多操作系统在实践中将具体含义归咎于特定回报
值(例如VMS)或具有严格限制的回报范围
值(例如Unix)。第三,这不是我在任何地方遇到的
的惯例。
This seems like a bad idea, for at least three reasons. First, C
only defines the meaning of three return values from main (to
wit, 0, EXIT_FAILURE, and EXIT_SUCCESS). Second, many operating
systems in practice ascribe specific meanings to specific return
values (e.g. VMS) or have a severely limited range of return
values (e.g. Unix). Third, this is not a convention that I have
encountered anywhere.
否则,如果这不是正确的事情,还应该做什么完成 ?
Otherwise, if this is not the right thing, what else should be done ?
典型的是向stderr写一条解释性消息,
返回EXIT_FAILURE。
-
char a [] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []
= {0x67dffdff,0x9aa9aa6a ,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},* p
= b,i = 24; for(; p + =!* p; * p / = 4)switch(0 [p ]& 3)case 0:{return 0; for(p - ; i - ; i - )case +
2:{i ++; if(i)break; else default:continue ; if(0)case 1:putchar(a [i& 15]); break;}}}
Typical would be to write an explanatory message to stderr and
return EXIT_FAILURE.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
" ju ********* *@yahoo.co.in" < ju ********** @ yahoo.co.inwrites:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
如果main()调用某个函数func()并且该函数返回
错误(错误),然后从main返回该值
(errno)是否有意义。 (如果主要不能继续进行)?
例如。
int main(无效)
{
int error;
if((error = func())0){
return(error); /* 这个可以吗。 ? * /
}
返回0;
}
If main() calls some function func() and that function returns
the error (errno), then does it make sense to return that value
(errno) from main. (in case main can''t proceed further) ?
eg.
int main(void)
{
int error;
if ((error = func()) 0) {
return(error); /* Is this Ok. ? */
}
return 0;
}
函数通常不会返回errno值。相反,许多
函数返回一个值,表示发生* some *错误并设置
全局变量''errno''。
errno值和
值之间通常没有特定的关系传递给exit()。这是有吸引力的,因为
0意味着没有错误。对于两者,但非零值不匹配,
无论是标准C还是我熟悉的任何系统。
-
Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>
" We必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
Functions don''t typically return errno values. Instead, many
functions return a value indicating that *some* error occurred and set
the global variable ''errno''.
There''s normally no particular relationship between errno values and
values passed to exit(). It''s tempting to thing that there is, since
0 means "no error" for both, but the non-zero values don''t match,
either in standard C or on any system I''m familiar with.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
9月19日星期三2007 00:38:43 -0700,Keith Thompson写道:
On Wed, 19 Sep 2007 00:38:43 -0700, Keith Thompson wrote:
" ju ********** @ yahoo.co.in" < ju ********** @ yahoo.co.inwrites:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
函数通常不返回errno值。相反,许多
函数返回一个值,表示* some *错误发生并设置
全局变量''errno''。
Functions don''t typically return errno values. Instead, many
functions return a value indicating that *some* error occurred and set
the global variable ''errno''.
M
M
这篇关于从main()返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!