[Help] Main

Posts 1–14 of 14 · Page 1 of 1
[Help] Main
I want to know what is the difference between those functions, and why there isn't only one. :

Code:
int main()
int main(int argc, char *argv[])
int main(int argc, char *argv)
int main(void)
void main()
void main(void)
Thank u
They all do the same thing.
I don't think the main function in C could be of void return type.

1st and 4th are the same.

2nd's main function can take multiple parameters where as the 3rd's can only take one ( why would you do that? ).

Read about arrays and command line arguments to get detailed explanations. Once you understand them this won't be a problem anymore.
thank u guyz (:
The 'standard' declaration of main, should be..
[php]int main( int argc, char *argv[] )[/php]
I tend to use..
[php]int main( int argc, char **argv )[/php]
because of the whole 'array decay to pointer' thing. I don't know, I like pointers.

The return value of main should be an int, notifying the user/OS whether the program terminated correctly or not (You should return the constants EXIT_FAILURE or EXIT_SUCCESS, as defined in <stdlib.h>)

The first argument to main is the amount of arguments passed in via the command line, the second argument is a pointer to the array of arguments.
This provides you enough functionality to retrieve information about how the program should run, and notify the OS how the program terminated.

The reason you see a few different declarations, as far as I can tell, is because some compilers aren't as strict. Don't expect your code to be portable if you don't conform to the standard.
I've always learnt that

void main();

Is wrong, wrong, wrong!
And should never be used, it's not defined in the ASNI standard, and thus it's not really part of the language. I think it's something that .net permits you to do. But a native compiler should not allow void main();
Quote Originally Posted by Gab View Post
I want to know what is the difference between those functions, and why there isn't only one. :

Code:
int main()
int main(int argc, char *argv[])
int main(int argc, char *argv)
int main(void)
void main()
void main(void)
Thank u
int main() <- bad
int main(int argc, char *argv[]) <- good
int main(int argc, char *argv) <- bad
int main(void) <- bad
void main() <- bad
void main(void) <- bad
int main(int argc, char **argv) <- good
int main(int argc, char argv[][]) <- good
Quote Originally Posted by Hell_Demon View Post
int main() <- bad
int main(int argc, char *argv[]) <- good
int main(int argc, char *argv) <- bad
int main(void) <- bad
void main() <- bad
void main(void) <- bad
int main(int argc, char **argv) <- good
int main(int argc, char argv[][]) <- good
I always use int main() ...

But thank u, i'll use int main(int argc, char *argv[])
Quote Originally Posted by Hell_Demon View Post
int main() <- bad
int main(int argc, char *argv[]) <- good
int main(int argc, char *argv) <- bad
int main(void) <- bad
void main() <- bad
void main(void) <- bad
int main(int argc, char **argv) <- good
int main(int argc, char argv[][]) <- good
But If you know your program won't be receiving any parameters through the command line?
Wouldn't one be eating space one doesn't need?
I mean, if we look at an assembly entry point (proc main) we don't see any special variables either...

Code:
main:
main proc

push eax
push ...
mov...
ca...
c...
...
main endp
END main
Quote Originally Posted by .::SCHiM::. View Post
But If you know your program won't be receiving any parameters through the command line?
Wouldn't one be eating space one doesn't need?
I mean, if we look at an assembly entry point (proc main) we don't see any special variables either...

Code:
main:
main proc

push eax
push ...
mov...
ca...
c...
...
main endp
END main
That's what we have the stack pointer for. :P
Quote Originally Posted by Void View Post
That's what we have the stack pointer for. :P
True, so C++ doesn't allocate extra space is you define it as (int argc, char *argv[]) because the (if any) arguments are just pushed to the stack? like you would with a normal function?
i also would prefer
int main(int argc, char **argv) instead of
int main(int argc, char *argv[])

because a array is nothing more than a pointer, and in my eyes **argv is better because if you're calling a function with this parameter **argv it is more clear that it should be a pointer to a pointer like:

char * c;
&c is a pointer to a pointer then

and with ** it is clear that you need a POINTER to a POINTER


But in the end there isn't any damn different so use what you want
One can only assume the calling convention is the standard cdecl (Caller maintains the stack)

AFAIK from my little experiments involving 'TinyPE' and 'TinyELF' files, 'main' isn't the real entry point - there's a proxy layer between the OS and your program's 'main' function and this is where the stack is maintained(?)
Thank you all for your answers
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us