使用返回
返回一个值
一个常用案例:从 main() 返回
#include <stdlib.h> /* for EXIT_xxx macros */
int main(int argc, char ** argv)
{
if (2 < argc)
{
return EXIT_FAILURE; /* The code expects one argument:
leave immediately skipping the rest of the function's code */
}
/* Do stuff. */
return EXIT_SUCCESS;
}
补充说明:
-
对于返回类型为
void的函数(不包括void *或相关类型),return语句不应具有任何关联的表达式; 也就是说,唯一允许的返回声明是return;。 -
对于具有非
void返回类型的函数,return语句不应在没有表达式的情况下出现。 -
对于
main()(仅适用于main()),不需要显式的return语句(在 C99 或更高版本中)。如果执行到达终止},则返回隐含值0。有些人认为省略这种情况是不好的做法; 其他人积极建议将其废除。
一无所获
从 void 函数返回
void log(const char * message_to_log)
{
if (NULL == message_to_log)
{
return; /* Nothing to log, go home NOW, skip the logging. */
}
fprintf(stderr, "%s:%d %s\n", __FILE__, _LINE__, message_to_log);
return; /* Optional, as this function does not return a value. */
}