C语言 `return 0x1;` 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3904219/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:39:23  来源:igfitidea点击:

What does `return 0x1;` mean?

creturn-valuehexreturn

提问by Rizo

When browsing the source of a project on web I've found some returnstatement in main that looks weird to me:

在网上浏览一个项目的源代码时,我发现returnmain 中有一些对我来说看起来很奇怪的语句:

int main()
{
    /* ... */
    return 0x1;
}

So main is returning 0x1 radix 16, but that's 1 radix 10! Shouldn't main return 0?

所以 main 正在返回0x1 radix 16,但就是这样1 radix 10!不应该主要返回0吗?

That is incorrect, right? By the way is it Okay to return 0x0?

那是不正确的,对吧?顺便说一句,可以return 0x0吗?

回答by JoshD

It returns 1. 0x1Is just a hex value of 1.

它返回 1。它 0x1只是一个 1 的十六进制值。

You are free to return 0x0, too. It's just a different representation of 0. You could use octal, too, if you like :)

您也可以自由返回 0x0。它只是 0 的不同表示。如果您愿意,也可以使用八进制:)

回答by Michael Madsen

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.

0x1 或 1 没有区别。这是相同的数字。因此,您也可以返回 0x0 - 这只是在代码中写入 0 的不同方式。

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 mainsignify failure, and if the program runs to the end, that's generally a sign of success - so you should return 0 in that case.

但是,假设 return 是主块中的最后一行代码,那么它可能不应该返回 1:来自main表示失败的非零返回代码,并且如果程序运行到最后,那通常是一个成功的标志 - 所以在这种情况下你应该返回 0。

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.

但是,完全有可能以相反的方式构建程序,因此这里返回 1 也可能是正确的。

回答by TK.

Simply put that translates to:

简单地说就是:

return 1;

by putting '0x' in front of the number allows you to enter Hexadecimal numbers into the source code e.g. 0xFF = 255

通过在数字前面放置“0x”,您可以在源代码中输入十六进制数字,例如 0xFF = 255

Its 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.

您的主函数可能会返回您想要的任何值,这样您就可以有效地记录可能(或可能不会)发生的任何错误情况。如果这个程序被一个询问返回值的进程调用,那么如果你将返回值更改为 0x0(或只是 0),那么调用程序可能会意外地改变它的行为。

回答by Arun

0x1is just another way of writing 1! The literal 1is same in decimal or hexadecimal. But its not always true, for example decimal 10is not equal to hexadecimal 10.

0x1只是另一种写作方式1!文字1在十进制或十六进制中相同。但它并不总是正确的,例如decimal 10不等于hexadecimal 10

In this example, main()is returning 1. Conventionally, any return value other than 0 is treated as unsuccesful completion.

在此示例中,main()返回 1。按照惯例,除 0 之外的任何返回值都被视为不成功的完成。

回答by AnT

Yes, 0x1(hexadecimal 1) is the same as 1(decimal 1). So, the above is equivalent to plain return 1.

是的,0x1(十六进制 1)与1(十进制 1)相同。所以,上面的等价于 plain return 1

mainis 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.

main不需要返回 0。main“应该”返回其作者希望它返回的任何内容。他们想返回 1 - 他们返回 1。他们想使用十六进制表示法 - 他们使用十六进制表示法。

Someone just felt like doing it that way. There's no other explanation.

有人只是觉得这样做。没有其他解释。

回答by Ole Dittmann

0x1 is just hexadecimal for 1. After compiling there is no difference. 0x0 would be eqivalent to 0.

0x1 只是 1 的十六进制。编译后没有区别。0x0 将等同于 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 函数的返回值。但是如果该程序被其他程序(例如安装程序)调用,它可能会被它们检查。如果一切正常,通常返回 0 并返回其他指示错误的内容。

回答by Ole Dittmann

I think this info is helpful to u?
Results according to http://codepad.org/

我认为这些信息对你有帮助?
结果根据http://codepad.org/

1) int main()  {  return 0x0; } 
 Output: No errors or program output.

2) int main()  {  return 1x2; } //anythingxanything results below error
  error: invalid suffix "x2" on integer constant

3) int main()  {  return 0x2; } //0xanything results :exit failure except for 0
  Exited: ExitFailure 1

Results according http://www.comeaucomputing.com/tryitout/

结果根据 http://www.comeaucomputing.com/tryitout/

except  2nd case it is compilng correctly(succeeded)

Results according

结果根据

GCC[g++]  ---  error  return 1x0; except 2nd case all correct
EDG_compiler ---  error  return 1x0; except 2nd case all correct
Sun compiler ---  error  return 1x0; except 2nd case all correct

By seeing this results ..IT is treating 1st and 3rd case as hexadecimal .ANd remaining 2nd case as different(not at all a number)expression or variable ..etc

通过看到这个结果..IT 将第 1 种和第 3 种情况视为十六进制。剩余的第二种情况作为不同的(根本不是数字)表达式或变量 ..etc

回答by phuclv

Do you really understand what a base number means?

你真的了解基数的含义吗?

Base-n number uses n digits from 0to n-1. (AbAb-1...A1A0)n= Ab*nb+ Ab-1*nb-1+ ...A1*n1+ A0*n0

Base-n 数字使用从0到的n 位数字n-1。(A bA b-1...A 1A 0) n= A b*n b+ A b-1*n b-1+ ...A 1*n 1+ A 0*n 0

So 1 is a valid hexadecimal character and 0x1means 1x160, which inherently equals to 1

所以 1 是一个有效的十六进制字符,0x1表示 1x16 0,它本质上等于 1

回答by Ignacio Vazquez-Abrams

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.g ERRORLEVELor $?.

的返回值main()作为可执行文件的返回值/错误代码传递给外部调用者,用于检入例如ERRORLEVEL$?

回答by Xaqron

It means : return 1

意思是:返回1

you can use numbers in hexadecimal format like this 0x...

您可以使用十六进制格式的数字,例如 0x...