Teen Programmers Unite  
 

 

Return to forum top

The old main () argument

Posted by slipknotclown [send private reply] at March 24, 2002, 07:27:51 AM

Ok does it matter if what way you declare main() ? I know a few ways but people say oh you shoudnt do itl ike that.

1: int main()
2: void main()
3: main()

Does it matter? or are some methods just known as bad practice if ya get me.

Posted by RedX [send private reply] at March 24, 2002, 08:08:41 AM

From a older C reference: void main(int argc, char *argv[]);
From The C++ Programming Language: int main(int argc, char *argv[]);

I think the int-version is the one specified by the ANSI-standard, but I'm not too sure of this.
In every case the last one is bad, because you can't know what a compiler will do with it. Some may see it as a void, others won't compile it.

RedX

Posted by metamorphic [send private reply] at March 24, 2002, 10:01:39 AM

Red is right, the old C / C++ programs used void main() with or without the 2 arrguments. The newer programs and the ANSI standard is to use int main() as the OS can get a return value of the program to check that it has exected properly. The last one most likley wouldn't complie as you have to declare return types for all functions in C/C++. Lastly, incase you didn't know slip, the 2 arrguments to function main are command line peram's for if you want to include perams for your program, if you dont use those 2 perams in your program, i would suggest leaving them out.

Short answer: Does it matter if what way you declare main() ? YES

Marc

Posted by slipknotclown [send private reply] at March 24, 2002, 02:02:31 PM

There may have been a bit of confusion I didnt literally mean arguments as in argv[] I meant as in when people dont agree with each other.

Posted by Mycroft [send private reply] at March 24, 2002, 02:54:50 PM

the ANSI standard is int main(void)

Posted by taubz [send private reply] at March 24, 2002, 10:32:28 PM

Technically it doesn't matter. It'll compile any which way, if it's syntactically correct. If you don't need the command line arguments, there's no real reason why you should include those parameters.

- taubz

Posted by buzgub [send private reply] at March 25, 2002, 03:10:05 AM

AFAIK the standard permits either int main(void) or int main(int, *char[]). If you're wondering, I left out the variable names because I they are called argc and argv only by convention; you could call them dog and cat if you really wanted to. If you want a definitive answer, check out http://groups.google.com/groups?q=g:thl1226870879d&hl=en&selm=87afvng1pa... which is a post on the topic sent to comp.lang.c, or http://www.eskimo.com/~scs/C-faq/q11.12.html from the comp.lang.c faq.

Just as a purely academic note, "main()" actually means "int main(void)" because a function definition with no return type is implicitly int, and one without an argument list has an implicit argument list of void.

Posted by ScaryGuy [send private reply] at March 26, 2002, 04:54:43 PM

You can send a message back to the OS with int main(void), or you dont have to with void main(void)

Posted by buzgub [send private reply] at March 27, 2002, 12:01:54 AM

The Standard says that declaring main as returning void invokes undefined behaviour. Beware the nasal demons. It might do what you want it to, or it might make your cat blow up.

Posted by gian [send private reply] at March 27, 2002, 12:35:07 AM

My cat exploded.

Posted by ScaryGuy [send private reply] at March 28, 2002, 03:58:12 PM

That would be funny, if my can't didn't explode either.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.