tags:

views:

185

answers:

7

Hi, 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 ?

+5  A: 

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
`int main(int argc, char* argv[])`?
Nathon
`int main(int argc, char* argv[], char *envp[])` ?
sharth
@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
+3  A: 

Yet another MSVC extension/bug!

Armen Tsirunyan
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.
Nathan Adams
<sarcasm>Wow, Microsoft even introduced the same bug into GCC!</sarcasm>
RichieHindle
@Richie GCC rejects `char main` and `void main` in C++.
jleedev
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
@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
@Richie: I edited, you can remove the downvote now
Armen Tsirunyan
@Armen Tsirunyan: Done, and again I apologise for my carelessness.
RichieHindle
@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
So, seeing as you think its a bug who's volunteering to contact Microsoft?
Nathan Adams
@David: `void` is **not** allowed as the return type for main in C. No idea where you got that idea.
R..
@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
+3  A: 

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:

http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c

wengseng
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
+2  A: 

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
Errors: `char` and `int` are different types, they are not *equivalent* by any means. `double` is implicitly convertible to `int` (just test: `double d = 1.5; int i = d;`).
David Rodríguez - dribeas
It isn't garbage. Local variables are initialized intentionally with 0xcc in the debug build. Catches bugs.
Hans Passant
Thanks for the comments. I've made an edit with those corrections.
Eric Mickelsen
A: 

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.

Nathon
A: 

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

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