int main() versus void main()

Pages: 123
Feb 22, 2010 at 2:10pm
Ok... Here is a program.

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main()
{
   return 0; // Could be return(0)
}

And here is another!

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main(
{
   // No return statement...
}



So, to me, both programmes work the same way! Why do both have different structures! What is the difference between int main() and void main()... And most importantly, which one is better???

I must mention that come compilers don't recognize void main() and return an error that main should compulsorily return an integer!

Please enlighten me! Especially I would like to direct this question to @Bazzy! He has always helped me on standards issue! But everyone else id invited to comment as well... In case this question is too dumb, do let me know :)
Feb 22, 2010 at 2:16pm
What is the difference between int main() and void main().

One is worth the death penalty, the other is not.

And most importantly, which one is better???

It's not a question of "better" it's a question of "legal"

I must mention that come compilers don't recognize void main() and return an error that main should compulsorily return an integer!

The authors of the compilers that do allow void main should be shot.

Note: you're 'allowed' to forget the return statement; somtimes the compiler adds it for you (stupid, IMO).
Last edited on Feb 22, 2010 at 2:16pm
Feb 22, 2010 at 2:18pm
void main() is not standard, period. There's no discussion to be had.

And by the way,
return 0; // Could be return(0)
It could also be return (((((((((((0)))))))))));. What's your point?
Feb 22, 2010 at 2:41pm

The authors of the compilers that do allow void main should be shot.

Hahahaha, then I guess Mircrosoft Visual Studio sucks big time!!! Anyway, thank God, g++ tells me straightaway, "No, you can't do that!!!"...

void main() is not standard, period. There's no discussion to be had.

Roger that, Officer! :)

It could also be return (((((((((((0)))))))))));. What's your point?

Hahahaha, no point actually. Was just feeling like adding a comment!

By the way, can I have a few technical details, why void main() must be illegal?
Feb 22, 2010 at 2:42pm
Could also be
return !!0;
or
return ~15;
because both of those would evaluate to all zeros (15 being 1111b; ~15 being 0b).
I'm not so sure about the bitwise ~ though.
Last edited on Feb 22, 2010 at 2:42pm
Feb 22, 2010 at 2:49pm
By the way, can I have a few technical details, why void main() must be illegal?

It's not illegal, it's just non-standard. The standard being the ANSI standard.
Here's some references:
http://users.aber.ac.uk/auj/voidmain.shtml
http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
http://www.eskimo.com/~scs/readings/voidmain.990301.html
Feb 22, 2010 at 2:52pm
~15 evaluates to -16.
Feb 22, 2010 at 2:58pm
My mistake. return 15^15; evaluates to 0. Anything xor'd with itself always evaluates to 0.
Feb 22, 2010 at 3:03pm
return 42/42; // hhgttg beats your silly XOR
Feb 22, 2010 at 3:20pm
return ((8 << 2) & (2 >> 8) ^ (4 * 4) - ~(8 - 1) - 24);

I built that with this:
1
2
3
4
#!/usr/bin/env python

while True:
    print(eval(raw_input("Expression: ")));

Python is excellent.
Last edited on Feb 22, 2010 at 3:21pm
Feb 22, 2010 at 4:19pm
Sigh...
Feb 22, 2010 at 4:21pm
helios wrote:
Your shenanigans don't affect me

I think someone lied :-)
Feb 22, 2010 at 4:28pm
It's the thread derailment, not the code.
Feb 22, 2010 at 4:38pm
Hehehe...
Feb 22, 2010 at 7:00pm
wtf. You guys fail at math.

return 42/42; // hhgttg beats your silly XOR

This evaluates to 1, not zero

return ((8 << 2) & (2 >> 8) ^ (4 * 4) - ~(8 - 1) - 24);

This doesn't evaluate to anything even CLOSE to zero.
Feb 22, 2010 at 7:09pm
Isn't this because void main() returns a random byte to whatever called it but int main() returns '0' when the program finishes w\o errors? Also because some processes that might call your program might also be waiting for a value to indicate that your program finished?

Finally ((8 << 2) & (2 >> 8)... EFSQ???
Last edited on Feb 22, 2010 at 7:10pm
Feb 22, 2010 at 7:19pm
Isn't this because void main() returns a random byte
No. void functions by definition return nothing. Not random data. Just nothing.

Also because some processes that might call your program might also be waiting for a value to indicate that your program finished?
The return value from main() is only used in shell scripting.
1. The script is designed around the programs it uses. If a program is known to not return anything meaningful, then the script should of course not use this value.
2. You can tell that a program finished because the script continues execution.
1
2
function();
//oh noes! how does i know function() returned??? 
Feb 22, 2010 at 7:22pm
Feb 22, 2010 at 7:41pm
This doesn't evaluate to anything even CLOSE to zero.

Yeah it does: http://i45.tinypic.com/2055q1f.png
Last edited on Feb 22, 2010 at 7:43pm
Feb 22, 2010 at 7:53pm
Your order of evaluation is completely screwed up.
But it does evaluate to zero, nevertheless.
Pages: 123