1

I am using Visual studio 2008. For below code

double main()
{
}

I get error:

error C3874: return type of 'main' should be 'int' instead of 'double'

But if i use below code

char main()
{
}

No errors. After running and exiting the output window displays

The program '[5856] test2.exe: Native' has exited with code -858993664 (0xcccccc00).

Question: Is compiler doing implicit cast from default return value of zero(integer) to char ?

how the code 0xcccccc00 got generated ?

Looks like last byte in that code seem to be the actual returned value. Why 0xcccccc is coming ?

wengseng
  • 1,330
  • 1
  • 14
  • 27
bjskishore123
  • 6,144
  • 9
  • 44
  • 66

7 Answers7

5

The correct way to do it, per the C++ standard is:

int main()
{
...
}

Don't change the return type to anything else or your code will not be C++ and you're just playing with compiler specific functionality. Those ccccc values in your example are just unitialized bytes (which the C allocator sets to 0xCC) being returned.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
  • 1
    `int main(int argc, char* argv[])`? – nmichaels Oct 14 '10 at 15:37
  • `int main(int argc, char* argv[], char *envp[])` ? – Bill Lynch Oct 14 '10 at 15:42
  • @sharth: that signature is not standard. The two standard signatures are: `int main()` and `int main( int, char** )` any other signature is non-standard. **EDIT**: After reviewing the standard, that signature is allowed by the standard, in fact *any* signature that returns int. It is just not portable as it is not guaranteed to be available in any other environment. – David Rodríguez - dribeas Oct 14 '10 at 16:09
4

The value returned from the main function becomes the exit status of the process, though the C standard only ascribes specific meaning to two values: EXIT_SUCCESS (traditionally zero) and EXIT_FAILURE. The meaning of other possible return values is implementation-defined. However there is no standard for how non-zero codes are interpreted.

You may refer to an useful post:

What should main() return in C and C++?

Community
  • 1
  • 1
wengseng
  • 1,330
  • 1
  • 14
  • 27
  • 1
    The interpretation of the value is irrelevant here. The standard determines that a) the code is incorrect --> return type of main **must** be int, and b) there is an implicit `return 0;` at the end of `main`. – David Rodríguez - dribeas Oct 14 '10 at 15:57
3

Yet another MSVC extension/bug!

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I don't know if it is necessarily a "bug" because you can also use void main. But, someone who has been programming in C++ for awhile knows that int main is the "right" way instead of the "Microsoft" way. – Natalie Adams Oct 14 '10 at 15:28
  • Wow, Microsoft even introduced the same bug into GCC! – RichieHindle Oct 14 '10 at 15:29
  • 2
    @Richie GCC rejects `char main` and `void main` in C++. – Josh Lee Oct 14 '10 at 15:33
  • 2
    The downvoter is a big Microsoft fan I guess... but which part do you disagree with? The standart clearly says that the return type of main SHALL BE int. Period. If it is not, then the compiler must issue a diagnostic. Therefore this is an MSVC BUG – Armen Tsirunyan Oct 14 '10 at 15:35
  • @jleedev, @Armen Tsirunyan: My humble apologies - I hadn't noticed the `C++` tag, and thought the OP was talking about C. I'll be more careful in future. (And SO now won't let me remove the downvote unless you edit the answer.) – RichieHindle Oct 14 '10 at 15:36
  • @Armen Tsirunyan: Done, and again I apologise for my carelessness. – RichieHindle Oct 14 '10 at 15:49
  • @Richie: Even in the case of C, where `void` is allowed as return type for main, --and without checking in the standard-- I doubt that `char` is a valid return type, and moreover, the standard clearly states that if no explicit return is used, the return of `main` is `0`, and the question states that it is yielding `0xcccccc00`, so that would be a bug anyway. – David Rodríguez - dribeas Oct 14 '10 at 16:02
  • So, seeing as you think its a bug who's volunteering to contact Microsoft? – Natalie Adams Oct 15 '10 at 01:01
  • 1
    @David: `void` is **not** allowed as the return type for main in C. No idea where you got that idea. – R.. GitHub STOP HELPING ICE Oct 15 '10 at 02:27
  • @R..: I stand corrected. I seemed to recall that C89 allowed for `void main(void)`. I do not have that version of the C standard, but a cursory search did not point to any *serious* source stating that `void` is allowed. So, thanks, I learned something new again :) – David Rodríguez - dribeas Oct 15 '10 at 08:05
2

The answer to your first question is sort of yes. A char is essentially a very small integral type, so the compiler is being (extremely) lenient. Double isn't acceptable because it's not an integral type. The 0xCCCCCC is memory that never got initialized (except for the purposes of debugging). Since ASCII characters can only have two hex digits, the conversion failed to set the first 24 bits at all (and just set the last 8 bits to 0). What an odd and undesirable compiler trick.

Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
2

About main function, $3.6.1/2 - "It shall have a return type of type int, but otherwise its type is implementation-defined."

As I understand, anything that mentions 'shall' in the standard document AND is not ahdered to by the code is an instant condition required to be diagnosed by the compiler, unless the Standard specifically says that such diagnosis is not required.

So I guess VS has a bug if it allows such a code.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
1

The main function is supposed to return an int. Not doing that means you're out in undefined territory. Don't forget to wave at the standard on your way past. Your char return probably works because char can be easily converted to an int. Doubles can certainly not. Not only are they longer (double the length) but they're floating point, which means you'll have 1s in wonky places.

Short answer: don't do that.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
1

It is probably because char will implicitly cast to an int, however double won't as there would be data loss.

(See here: http://msdn.microsoft.com/en-us/library/y5b434w4%28v=VS.71%29.aspx for more info)

However you don't see the conversion problem because the compiler catches the worst sin (as stated in the other answers) of using a non standard return type.

Nick Meldrum
  • 1,141
  • 1
  • 10
  • 27