Archive

Posts Tagged ‘C’

void main()? No int main()!

September 16, 2009 2 comments

Today at the morning, on my way to the bus station, I asked my self “What should I post today?”

Few years ago (about 3-4) I posted a question about C on one of the forums. I also included my code, it had void main() in it. People started to yell at me, that this is bad and not according the standard. Later I discovered that ANSI says that main must return a value, so I took it as a rule to follow the standard. I couldn’t really find any reason why void main() is bad, as it worked all the time no matter on what OS.

So, today when I know assembly I could find the answer to the holy war between int main and void main.

So what is the difference between void main() and int main()?

Well first of all lets take a look at the assembly code we get after we compile each version of main:

$cat foo.c
int main() { return 0; }
gcc foo.c -o foo
objdump -d foo
08048374 <main>:
08048374:     55                                 push %ebp
8048375:       89 e5                            mov %esp,%ebp
8048377:       b8 00 00 00 00     mov $0x0,%eax
804837c:       5d                                 pop %ebp
804837d:      c3                                  ret
804837e:      90                                  nop
804837f:       90                                 nop

And the second one

$cat foo.c
void main() {}
gcc foo.c -o foo
objdump -d foo
08048374 <main>:
8048374:        55                         push %ebp
8048375:        89 e5                   mov %esp,%ebp
8048377:       5d                          pop %ebp
8048378:        c3                         ret
8048379:        90                        nop
804837a:        90                        nop
804837b:        90                        nop
804837c:        90                        nop
804837d:       90                        nop
804837e:       90                        nop
804837f:        90                       nop

So the only difference in the line mov $0x0, $eax

How does return mechanism works?

So what really happens when you write return 5? This generates an assembly code of mov eax, 0x5. So we can tell for sure that the value in eax register is the value that the function returns. So why int and not void? The standard says that any application must return an error code to the OS, while 0 represents that the execution was successful and everything else represents an error. What the OS does with this value? Nothing special to be honest, but assume the following scenario:

Application A needs to execute application B. A can not continue without B finishes, however B can fail, in that case A have to fail also. How A will know that B failed? Exactly! It will check the value of the eax register after B finished to execute. If we used void main(), and the end of main eax will be undefined and may have any value! So its like Russian roulette 😛 This why we have to write int main()!

I hope this post was useful and you learned something new! Leave comments and have a good day 🙂

Categories: Programming Tags: , ,