可以通过将函数类型从int更改为void来避免放置return 0吗?

huangapple go评论52阅读模式
英文:

Can I avoid putting return 0 by changing the function type from int to void?

问题

int main()
{
    ...
    return 0;
}

void main()
{
    ...
}

我在两种情况下都放了printf("Hello"),并且得到了相同的结果。不过,void版本编译时间较长。我想知道它们之间有什么区别,如果我使用int main并加上return,或者只是使用void main而不加return,会有什么不同。
英文:

int main()
{
...
return 0;
}

void main()
{
...
}

I put printf("Hello") in both cases and I got the same result. The void version took longer to compile though.. I was wondering what's the difference and does it make any difference if I go with int main and then put return or just put void main to avoid putting return.

答案1

得分: 1

以下是代码部分的中文翻译:

以下是一个示例代码:

#include <stdio.h>

int main() {
    printf("hello world\n");
    return 0;
}

编译并运行上述代码:

$ gcc -Wall test.c
$ ./a.out
hello world
要获取退出状态,请运行 `$?`,它将返回 0
$ echo $?
0

现在将返回值更改为其他整数,如 1

#include <stdio.h>

int main() {
    printf("hello world\n");
    return 1;
}

编译并执行:

$ gcc -Wall test.c
$ ./a.out
hello world
$ echo $?
1               # 返回退出状态为 1

现在尝试仅使用 main 而不使用 return

#include <stdio.h>
main() {
    printf("hello world\n");
}

编译器会发出警告并使用默认类型 int

$ gcc -Wall test.c
test.c:3:2: 警告:返回类型默认为 'int' [-Wimplicit-int]
3 | main() {
| ^~~~
$ ./a.out
hello world
$ echo $?
0               # 退出状态为 0

现在尝试使用 void main 而不使用 return

#include <stdio.h>
void main() {
    printf("hello world\n");
}

编译器警告 main 的返回类型不是 int

$ gcc -Wall test.c
test.c:3:6: 警告:'main' 的返回类型不是 'int' [-Wmain]
3 | void main() {
| ^~~~
$ ./a.out
hello world
$ echo $?
12

在你的程序中包含退出代码是一个良好的实践,以了解你的程序是否成功执行。
英文:

Take an example of below code:

#include &lt;stdio.h&gt;
int main() {
printf(&quot;hello world\n&quot;);
return 0;
}

compile and run above code

$ gcc -Wall test.c
$ ./a.out
hello world
to get exit status run `$?` it will return 0
$ echo $?
0

Now change return to some other integer like 1

#include &lt;stdio.h&gt;
int main() {
printf(&quot;hello world\n&quot;);
return 1;
}

Compile and execute

$ gcc -Wall test.c
$ ./a.out
hello world
$ echo $?
1               # it returned exit status as 1

Lets try with main only and no return:

#include &lt;stdio.h&gt;
main() {
printf(&quot;hello world\n&quot;);
}

Compiler warns and use the default type int

$ gcc -Wall test.c
test.c:3:2: warning: return type defaults to ‘int’ [-Wimplicit-int]
3 |  main() {
|  ^~~~
$ ./a.out
hello world
$ echo $?
0               # exit status 0

Now lets try with void main and no return

#include &lt;stdio.h&gt;
void main() {
printf(&quot;hello world\n&quot;);
}

compiler warns about return type of main is not int

$ gcc -Wall test.c
test.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
3 | void main() {
|      ^~~~
$ ./a.out
hello world
$ echo $?
12

Its a good practice to include exit code in your program to know is your program is executed successfully

huangapple
  • 本文由 发表于 2023年1月9日 02:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定