xcode C : 返回类型默认为 Int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8632918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C : Return Type Defaults to Int
提问by Justian Meyer
I'm trying to follow "Cocoa and Objective C: Up and Running" by OReilly (a great read by the way), but I'm consistently bugged with these annoying warnings.
我正在尝试遵循 OReilly 的“Cocoa and Objective C: Up and Running”(顺便说一句,读得很好),但我一直被这些烦人的警告所困扰。
For example: this code segment (p.56-57)...
例如:此代码段 (p.56-57)...
#include <stdio.h>
#include <stdlib.h>
main() {
int total = 81;
float ratio = 1.618;
char* result;
asprintf (&result, "total: %i, ratio: %1.3f", total, ratio);
printf ("%s \n", result);
free (result);
}
Throws me this...
扔给我这个...
/Users/justianmeyer/Desktop/test.c:5: warning: return type defaults to ‘int'
/Users/justianmeyer/Desktop/test.c: In function ‘main':
/Users/justianmeyer/Desktop/test.c:18: warning: control reaches end of non-void function
total: 81, ratio: 1.618
I've been programming for a while now, but am completely new to C/Objective C as of yesterday. I've been reading that undeclared methods are expected to accept/return an int by default, but isn't the main()
method supposed to be automatically declared? Why would it need to be defaulted to an int? Furthermore, is main()
supposed to return an int? What's the use in that? Where is the return value received?
我已经编程一段时间了,但从昨天开始我对 C/Objective C 还是个新手。我一直在读到,默认情况下未声明的方法应该接受/返回一个 int,但该main()
方法不应该自动声明吗?为什么需要将其默认为 int?此外,main()
应该返回一个int?那有什么用?收到的返回值在哪里?
Also: Is there any way to create a ".c" file in XCode 4.2 without including it in a project? Right now I'm using TextMate for the examples, but I want to become as familiar with XCode as possible over the course of this book.
另外:有没有办法在 XCode 4.2 中创建一个“.c”文件而不将它包含在项目中?现在我正在使用 TextMate 作为示例,但我希望在本书的过程中尽可能熟悉 XCode。
回答by Rob
I don't know ObjectiveC but in standard C you must declare the return type and the parameters for main. So:
int main(void)
should work. Most (all) operating systems expect a return of int from main.
我不知道 ObjectiveC 但在标准 C 中你必须声明 main 的返回类型和参数。所以:
int main(void)
应该工作。大多数(所有)操作系统都期望从 main 返回 int。
The return value from main is the exit code for the program to indicate whether it ended normally or otherwise.
main 的返回值是程序的退出代码,用于指示程序是否正常结束。
回答by Steve Wellens
Warning are your friends.
警告是你的朋友。
It's telling you the function returns int but you are not returning anything so there is a potential problem.
它告诉您该函数返回 int 但您没有返回任何内容,因此存在潜在问题。
回答by dreamlax
In older C, any function could be defined without an explicit return type, and by default, it was assumed to return int
. This was also true for function parameters. In more recent versions of the C standard, implicit-inthas been removed (paragraph 5 of Foreword).
在旧的 C 中,任何函数都可以在没有显式返回类型的情况下定义,默认情况下,它被假定为 return int
。对于函数参数也是如此。在 C 标准的更新版本中,隐式 int已被删除(前言第 5 段)。
Also, any function that has a return type other than void
(meaning the function does not return a value) must have a return
statement, otherwise your program is not conforming. Conformance is important if you want your program to run in a well-defined manner. main
is a special exception to the return
-statement-rule, whereby it does not needto have a return statement. If no return statement is provided, then the main
function implicitly returns 0 (section 5.1.2.2.3).
此外,任何具有除void
(意味着该函数不返回值)以外的返回类型的函数都必须有一个return
语句,否则您的程序将不符合要求。如果您希望程序以明确定义的方式运行,一致性很重要。main
是一个特殊的例外return
语句来规则,因此它并不需要有一个return语句。如果未提供 return 语句,则该main
函数隐式返回 0(第 5.1.2.2.3 节)。
Your compiler warned you that you have not explicitly specified a return type, and also that the control reaches the end of a non-void function without returning a value. The former can be easily remedied by putting int
before main
in your code. The latter—even though main
is an exception to the rule—can likewise be remedied by putting in a return statement in anyway, just to be consistent with other non-void functions.
您的编译器警告您没有明确指定返回类型,并且控件到达非空函数的末尾而没有返回值。前者可以通过在代码中放入int
before来轻松解决main
。后者——尽管main
是规则的一个例外——同样可以通过以任何方式放入 return 语句来补救,只是为了与其他非 void 函数保持一致。
回答by Khanan Grauer
To fix this you need int main() and return 0; at the end:
要解决此问题,您需要 int main() 并返回 0;在末尾:
#include <stdio.h>
#include <stdlib.h>
int main() {
int total = 81;
float ratio = 1.618;
char* result;
asprintf (&result, "total: %i, ratio: %1.3f", total, ratio);
printf ("%s \n", result);
free (result);
return 0;
}
回答by abcde123483
A C function can be written as
AC函数可以写成
<return type a> <function name>( <type 1> <parameter 1>, .. ) {
...
return <element of type a>;
}
However can be left out, but by the set of rules your compiler plays by it is advised to specify a return type.
但是可以省略,但根据您的编译器所遵循的一组规则,建议指定返回类型。