gpt4 book ai didi

c - 使用返回 0;在 C 中的 main() 中

转载 作者:太空宇宙 更新时间:2023-11-04 05:02:34 25 4
gpt4 key购买 nike

<分区>

我正在阅读 Brian Kernighnan 和 Dennis Ritchie 的《C 语言编程》(第 25 页底部)

这里作者引用:

The value that power computes is returned to main by the return statement. Any expression may follow return: return expression ;

但是在他上面提供的代码中:

#include <stdio.h>
int power(int m , int n);

main()
{
int i;
for (i = 0; i < 10; ++i)
printf ( " %d \t %d \t %d \n ", i, power( 2 , i), power( -3 , i));
return 0;//no. 1
}

int power(int m , int n)
{
int i, p ;
p = 1;
for (i = 1; i <= n; ++i)
p = p * m;
return p; //no. 2
}

到这里我明白了为什么他在函数power中使用了2.return p;(即获取p的值)但是为什么他使用了1.return 0; ??我尝试删除行 return 0; 但它仍然像我想的那样工作,是不是我遗漏了什么??

[更新]很抱歉之前没有包括这个,但我已经知道了这么多:
书中引用:

You may have noticed that there is a return statement at the end of main. Since main is a function like any other, it may return a value to its caller, which is in effect the environment in which the program was executed. Typi- cally, a return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions. In the interests of simplicity, we have omitted return statements from our main functions up to this point, but we will include them hereafter, as a reminder that programs should return status to their environment.

感谢@Pascal,我能够理解 return p 的区别;并返回 0;然而,我的意图永远不会知道 return 0 是什么; was but why return was used 也知道两者return语句的区别……

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com