`return 0x1;` 是什么意思?
在网络上浏览项目的源代码时,我发现 main 中的一些 return
语句对我来说看起来很奇怪:
int main()
{
/* ... */
return 0x1;
}
所以 main 返回 0x1 radix 16
,但那是 1 基数 10
! main 不应该返回 0
吗?
这是不正确的,对吧?顺便问一下,返回 0x0
可以吗?
When browsing the source of a project on web I've found some return
statement in main that looks weird to me:
int main()
{
/* ... */
return 0x1;
}
So main is returning 0x1 radix 16
, but that's 1 radix 10
! Shouldn't main return 0
?
That is incorrect, right? By the way is it Okay to return 0x0
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
它返回 1。
0x1
只是一个十六进制值 1。您也可以返回 0x0。它只是 0 的不同表示形式。如果您愿意,您也可以使用八进制:)
It returns 1.
0x1
Is just a hex value of 1.You are free to return 0x0, too. It's just a different representation of 0. You could use octal, too, if you like :)
0x1 或 1 没有区别。这是同一个号码。因此,您也可以返回 0x0 - 这只是在代码中编写 0 的另一种方式。
但是,假设 return 是主块中的最后一行代码,那么您是对的,它可能不应该返回 1:来自
main
的非零返回代码表示失败,并且如果程序运行到最后,这通常是成功的标志 - 因此在这种情况下您应该返回 0。然而,完全有可能以相反的方式构造一个程序,因此这里返回 1 也可能是正确的。
0x1 or 1 makes no difference. It's the same number. Consequently, you can return 0x0 as well - it's just a different way of writing 0 in your code.
However, assuming that return is the last line of code in your main block, you're right that it should probably not be returning 1: non-zero return codes from
main
signify failure, and if the program runs to the end, that's generally a sign of success - so you should return 0 in that case.However, it is entirely possible to structure a program the other way around, so it is therefore also possible that returning 1 is correct here.
简单地说,这意味着:
通过将
0x
放在数字前面,它允许您在源代码中输入十六进制数字,例如 0xFF = 255您的主函数可以返回您想要的任何值,这您可以有效地记录可能发生(或未发生)的任何错误情况。如果该程序由询问返回值的进程调用,那么如果将返回值更改为 0x0(或只是 0),则调用程序可能会意外更改其行为。
Simply put that translates to:
by putting
0x
in front of the number it allows you to enter Hexadecimal numbers into the source code e.g. 0xFF = 255It's possible for your main function to return any value you want, this way you can effectively document any error conditions that may (or may not) have happened. If this program was called by a process that interrogates the return value, then if you change the return value to 0x0 (or just 0) then the calling program might change its behaviour unexpectedly.
是的,
0x1
(十六进制 1)与1
(十进制 1)相同。因此,上面的内容相当于普通的return 1
。main
不需要返回 0。main
“应该”返回其作者希望它返回的任何内容。他们想要返回 1 - 他们返回 1。他们想要使用十六进制表示法 - 他们使用十六进制表示法。有人只是想这样做。没有其他解释了。
Yes,
0x1
(hexadecimal 1) is the same as1
(decimal 1). So, the above is equivalent to plainreturn 1
.main
is not required to return 0.main
"should" return whatever its author wants it to return. They wanted to return 1 - they returned 1. They wanted to use hexadecimal notation - they used hexadecimal notation.Someone just felt like doing it that way. There's no other explanation.
0x1
只是1
的另一种书写方式!文字1
在十进制或十六进制中是相同的。但它并不总是正确的,例如十进制 10 不等于十六进制 10。在此示例中,
main()
返回 1。按照惯例,除 0 之外的任何返回值都将被视为未成功完成。0x1
is just another way of writing1
! The literal1
is same in decimal or hexadecimal. But its not always true, for exampledecimal 10
is not equal tohexadecimal 10
.In this example,
main()
is returning 1. Conventionally, any return value other than 0 is treated as unsuccesful completion.main()
的返回值作为可执行文件的返回值/错误代码传递给外部调用者,用于检查,例如ERRORLEVEL
或$?< /代码>。
The return value of
main()
is passed up to the external caller as the return value/error code of the executable for checking in e.gERRORLEVEL
or$?
.这意味着:返回 1
您可以使用十六进制格式的数字,例如 0x...
It means : return 1
you can use numbers in hexadecimal format like this 0x...
0x1 只是 1 的十六进制。
编译后没有什么区别。
0x0 相当于 0。
main 函数的返回值通常不被系统使用。
但是,如果该程序被其他程序(例如安装程序)调用,则它们可能会检查该程序。
如果一切正常,通常会返回 0,如果出现其他错误,则返回其他值。
0x1 is just hexadecimal for 1.
After compiling there is no difference.
0x0 would be eqivalent to 0.
The return value of the main function is normally not used by the system.
But if the program is called by other programs (eg. installers) it might get checked by them.
It is common to return 0 if everything is ok and something else to indicate an error.
“main 返回
0x1 基数 16
,但那是1 基数 10
” 是什么意思?这里的基数是 16,因为 C 指定
0x
前缀表示十六进制数。你真的了解基数的含义吗?以 n 为基数的数字使用从
0
到n-1
的 n 个数字。 (AbAb-1...A1A0)n = Ab×nb + Ab-1×nb-1 + ...A 1×n1 + A0×n0因此 1 是有效的十六进制字符,
0x1
表示 1x160,本质上等于 1返回值是程序的 退出代码,因此可以可以是有效范围内的任意值。通常返回 0 表示程序退出且没有任何错误。请参阅C 和 C++ 中 return 0 的意义是什么?
What do you mean by "main is returning
0x1 radix 16
, but that's1 radix 10
"?The radix here is 16 because C specifies that the
0x
prefix denotes a hexadecimal number. Do you really understand what a base number means?Base-n number uses n digits from
0
ton-1
. (AbAb-1...A1A0)n = Ab×nb + Ab-1×nb-1 + ...A1×n1 + A0×n0So 1 is a valid hexadecimal character and
0x1
means 1x160, which inherently equals to 1The return values is the exit code of the program, so it can be any values in the valid range. Typically returning 0 means the program exited without any errors. See What is the significance of return 0 in C and C++?
我认为这些信息对您有帮助吗?
结果根据 http://codepad.org/
int main() { return 0x0; }
输出:没有错误或程序输出。
int main() { 返回 1x2; } //anythingxanything 会导致以下错误
int main() { return 0x2; } //0x除0之外的任何内容都会导致退出失败
结果根据 http://www.comeaucomputing .com/tryitout/
结果通过
查看此结果,IT 将第 1st 和 3rd 情况视为十六进制,并且剩余 2 nd case 为不同的(根本不是数字)表达式或变量...等
I think this info is helpful to you?
Results according to http://codepad.org/
int main() { return 0x0; }
Output: No errors or program output.
int main() { return 1x2; } //anythingxanything results in the below error
int main() { return 0x2; } //0xanything results in exit failure except for 0
Results according to http://www.comeaucomputing.com/tryitout/
Results according
By seeing this results, IT is treating the 1st and 3rd case as hexadecimal, AND the remaining 2nd case as different (not at all a number) expression or variable... etc
0x1 就十六进制而言就是 1。
0x 表示该数字为十六进制 形式。
如果尝试以下代码,您可以获得相同的行为:
0x1 is simply 1 in terms of hex.
0x signifies that the number is in hex form.
You can get the same behavior if you try the below code: