By the way, void main is just plain wrong. Don't use it.


What's wrong with void main?

Here is the assembly output of my code, plus I have the opportunity of using my own C run-time startup module eliminating all those regular initialization routines.


_TEXT SEGMENT
_main PROC NEAR

; 7 : begin

push esi

; 8 : int i;
; 9 : for(i=0;i<5;++i)

mov esi, 5
$L772:

; 10 : begin
; 11 : printf("begin&end instead of braces :)\n");

push OFFSET FLAT:$SG775
call _printf
add esp, 4
dec esi
jne SHORT $L772
pop esi

; 12 : end
; 13 : end

ret 0
_main ENDP
_TEXT ENDS
Posted on 2004-10-18 14:57:57 by Vortex
What's wrong with void main?

void main is illegal both in C and in C++. The fact that your compiler accepts the code is purely incidental. In C void main means "undefined behaviour". In C++ void main means an "ill-formed program". main is always supposed to return an int.

Why is that?
Because when the program is run, main can be thought as being called like this:

exit(main());

(Of course, arguments might also be passed.)
Posted on 2004-10-18 15:13:45 by death
Thanks for the technical info.

Still, it's possible to kick away "exit(main());" ( simplified C run-time startup module )
Posted on 2004-10-18 15:25:11 by Vortex
Still, it's possible to kick away "exit(main());" ( simplified C run-time startup module )

Not very portable is it? When you play with a high level language, I am of the opinion that you should play by the language rules, not break them.

This was only a reason for your 'why'. It still doesn't mean void main is legal.

Why would you want to use void main anyway?
Posted on 2004-10-18 15:31:48 by death
Using your own C run-time startup module has nothing to do with portability if you are running on Windows platform:

http://www.asmcommunity.net/board/viewtopic.php?t=19315

You can use ExitProcess instead of exit, this technique is also used by Pelle's C compiler.

I have no any problem with the variants of int main :)
Posted on 2004-10-18 15:42:47 by Vortex
Using your own C run-time startup module has nothing to do with portability if you are running on Windows platform


That is incorrect. Since the compiler generates your program's code, it might need specialized startup initialization. It doesn't matter if you code for Windows platform or not.

The C and C++ languages don't even allow you (the compiler user) to call main. When you're writing your own startup code, you're changing role to the compiler library writer. And this isn't portable when you change compilers/compiler versions.
Posted on 2004-10-18 15:48:08 by death
:-D DAMN! When I read the title, I thought you were referring to the braces used to fix teeth alignment. I said to myself "WTF is kind(a) kind(b) choices?"

I didn't read the body content.

:-D :-D :-D LOL!

When I code in Java I apply with lowercase letters on the first letter of the function name. Every other language I code in uses with uppercase letters.

I prefer B but I like A also. can I vote twice? LOL
Posted on 2004-10-18 15:48:51 by arkane
Death,

If you check my examples, you will see that they run with full success.
I am not the first person who coded special C run-time startup modules. You can check Jeremy Ibsen's startup module at:

WCRT v1.12 - Win32API CRT

http://www.ibsensoftware.com/download.html

Notice that my own module is coded in full asm.

I don't pretend that my module will run with every C code but still it does the job to create tiny C executables.
Posted on 2004-10-18 15:57:27 by Vortex
I assume you mean J?rgen Ibsen (Jibz) :)
In any case, I did not say your code is useless. I just said it's not as portable as you might think. Jibz writes his startup code for Visual C++. Your code might work with several compilers, yet, it won't work with every Windows compiler (or every C program, if you say).
Posted on 2004-10-18 16:07:20 by death
My apologies, J?rgen Ibsen

I am not worried with portability as my code works with the most popular compilers. As my own module doesn't satisfy always the portability rule,the portability doesn't mean everything like tiny C executables. :)
Posted on 2004-10-18 16:11:49 by Vortex
Correction to self:
The C and C++ languages don't even allow you (the compiler user) to call main.

Calling main in C is allowed.
Posted on 2004-10-18 16:25:46 by death
void main() is by no means illegal. It is c-call, and of ALL cases I know, you need the return value JUST once - when making makefiles/batch files where execution can be cancelled due to error. I always use void main() coz hell I don't want to define the args. And when I want to return a value - I ExitProcess.
btw in my ARM-elf postlinker, it's
long main (long msg,long wParam,long lParam);
lol Only here I use arguments and return for main() :-D

I prefer the a)braces , but if the current function has more than 10 parameters, I use b. I hate wasting lines of code, because this makes harder to scroll the source. And often put spaces like in "x += 1;"
Posted on 2004-10-18 17:31:13 by Ultrano
void main() is by no means illegal

Can you show me references to back up your claim?

I prefer the a)braces , but if the current function has more than 10 parameters, I use b.

If a function has more than 10 parameters, something is seriously wrong.
Posted on 2004-10-18 17:41:41 by death
you're wrong and wrong
lol cmn dude what's with the atitude?
Ok, I spent a few seconds to show you
void main{
printf("yo, see?");
}

now, disassembled it, the startup code does some initialization, then :
call 00401000 ; call main
add esp,0Ch ; see ???

and main() is:
push addr str1
call printf
pop ecx
ret


after that it preserves eax on stack, does
GetCurrentProcess, TerminateProcess with the preserved eax. Couldn't you do it on your own instead of jumping immediately ??????

In a function with 10 parameters things are not wrong. This is ARM code I'm talking about, and I know my job pretty well :)
please next time count to 10 and try to understand others before you reply
Posted on 2004-10-18 18:00:52 by Ultrano
You might want to read my first reply to Vortex. This is my last posting about this issue here.
Posted on 2004-10-18 18:07:05 by death
yes I read that and I explained that actually you never need that value. Ok, it may be incidental, but if it weren't then we wouldn't be using void main(void). You are free to code whatever and however you want, so I chose the simpler declaration, that doesn't turn me down on coding the app even on the first line of code. And when I'm teaching some friends C coding, I don't want to tell them legends from the start. They'd better concentrate on the stuff that matters, like I do. :)
And - one'd better not jump to conclusions if one doesn't know the guts of the matter ;) . I used to do that a lot, but Palm coding is strict and limited, and thus taught me well.

Ah, back on the topic - no matter how the braces are put, today at uni I learnt I need them lol. I used to hate them but HLL without it is damn boring and hard to read - we study algorithms first in pseudocode :o and with my bad handwriting it's hard to tell lines and rows apart lol. Damn soccer for injuring my hand lol
Posted on 2004-10-18 18:37:32 by Ultrano
If you're going to use a non-standard entrypoint (ie, void return type in this example), simply don't call it main().
Posted on 2004-10-19 02:47:51 by f0dder
I do not use neither kind (a) nor kind (b) in my code because none of assemblers known to me uses curly braces as block delimitator. In fact, a line starting with the closing brace or right parenthesis hurts my education.

Though assembler programs tend to be slim and long, indentation helps to visualize vertical block structure of source. Human eye is able to quickly scan down the screen keeping the same distance from left edge. So I'm used to keep begin/end orders such as PROC/ENDP, STRUC/ENDS, IF/ELSE/ENDIF on the same indent level. It is easier then to skip the entire block on second pass of reading.
Similar indentation is useful in other constructs which need to maintain block structure - PUSH/POP, CLI/STI etc.

So if I weren't asm programmer on asmcommunity board, I would vote kind (b).
Posted on 2004-10-19 05:10:23 by vit$oft
If a function has more than 10 parameters, something is seriously wrong.


Maybe you would like to look at this:


Oleaut32.lib

_OleCreatePropertyFrame@44

Ole32.lib

_OleCreateFromFileEx@52
_OleCreateLinkFromDataEx@48
_OleCreateLinkToFileEx@48
_OleCreateEx@48


All these functions have more than 10 parameters.
Posted on 2004-10-19 12:56:26 by Vortex
Just because MS does it doesn't mean it's a good thing.
Posted on 2004-10-19 13:04:54 by death