18

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Rizo
  • 3,003
  • 5
  • 34
  • 49
  • 16
    Believe it or not - some of us think that it's sometimes more natural to think in base 16 than base 10! – Dan Byström Oct 11 '10 at 07:24
  • 27
    To me, it's just some programmer trying to show off. – Nicolas Viennot Oct 11 '10 at 09:26
  • It appears that all answers in this thread are recently down-voted... Wondering whats going on.. – Arun Oct 11 '10 at 11:02
  • [meta thread regarding these downvotes](http://meta.stackexchange.com/q/67255/146482) – Tobias Kienzler Oct 11 '10 at 11:14
  • 1
    One note about returning 0 vs 1: It could be the design of the program that it defaults to returning 1 (indicating an error). Check somewhere else in `main()` to see if there's a case where it returns 0. Something like: `if (test1) { return 0x0; } else if (test2) { return 0x0; } return 0x1;`... There's likely a reason for it to be returning 1 (be it for convention, because the program should always be treated as an error, conditional returns, etc)... – ircmaxell Oct 11 '10 at 16:22

11 Answers11

30

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 :)

JoshD
  • 12,490
  • 3
  • 42
  • 53
5

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.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
3

Simply put that translates to:

return 1;

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

It'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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
TK.
  • 46,577
  • 46
  • 119
  • 147
2

Yes, 0x1 (hexadecimal 1) is the same as 1 (decimal 1). So, the above is equivalent to plain return 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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

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

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

Arun
  • 19,750
  • 10
  • 51
  • 60
1

What do you mean by "main is returning 0x1 radix 16, but that's 1 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 to n-1. (AbAb-1...A1A0)n = Ab×nb + Ab-1×nb-1 + ...A1×n1 + A0×n0

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

Shouldn't main return 0

The 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++?

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

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 ERRORLEVEL or $?.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

It means : return 1

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

Xaqron
  • 29,931
  • 42
  • 140
  • 205
1

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.

Ole Dittmann
  • 1,764
  • 1
  • 14
  • 22
1

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

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

    Output: No errors or program output.

  2. int main() { return 1x2; } //anythingxanything results in the below error

    error: invalid suffix "x2" on integer constant
    
  3. int main() { return 0x2; } //0xanything results in exit failure except for 0

    Exited: ExitFailure 1
    

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

except  2nd case it is comping 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 the 1st and 3rd case as hexadecimal, AND the remaining 2nd case as different (not at all a number) expression or variable... etc

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

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:

int main()
{
    /* ... */
    return 1;
}