What’s the difference between:
main()
int main()
void main()
is there another way of initializing main?
Thx :)
Printable View
What’s the difference between:
main()
int main()
void main()
is there another way of initializing main?
Thx :)
What’s the difference between:
main()
int main()
void main()
I dont know about main(), but int main() returns an integral, void main() does not return anything.
is there another way of initializing main?
I supose you can do bool main(), double main(), float main() and so on.....
the one other way of initializing main() is
which makes use of the outside operating system, for things like command line arguments.Code:int main(int argc,char *argv[])
main() = void main()
Void main is bad. goto search in the upper right corner. Type in void main and you'll see why.
In short. Void main doesn't return a value, and sometimes it returns a random value to the operating system, in all cases this is BAD.
int main() = GOOD! Look for a user named Salem, look at his quotes.
int main() returns a number (0 on success) and the OS will be able to tell if the program ran smoothely based on the numbers returned.
as for returning different types... depends on the OS, but usually NO. char main() I guess would work cuz its actually a short unsigned int.
WinMain() returns something besides an int, I think
For all intended purposes, stick to int main()
-LC
>>char main() I guess would work cuz its actually a short unsigned int.
void main( ) might also work. But it's not actually legal C/C++. The only ways that you can define main are:
Code:int main( void )
AND
int main( int argc, char **argv )
Thx. i've came across smething else
int main(void)
What's this?
Thx Salem. got another thing i don't quite understand
what does argc and *argv[] means n what do they do.Quote:
int main(int argc,char *argv[])
argc is the number of arguments supplied to the program, *argv[] is the pointer to the argument strings.Quote:
Originally posted by nightingale
Thx Salem. got another thing i don't quite understand
what does argc and *argv[] means n what do they do.
I've hurd that void main () is legal but it should only be used when you don't want your program to return a value, and the only reason you shouldn't want this is when you make programs witch should run in the back of the OS but I could be wrong.....
>>I've hurd that void main () is legal
void main( ) is never legal. Period.
>>I've hurd that void main () is legal
>void main( ) is never legal. Period.
Not true for C in general -- but correct for hosted environments (if you're using a freestanding environment, you probably know it; if you've never heard of the distinction, you're probably using a hosted environment*).
Lets say the program is called "hello"Code:int main(int argc, char* argv[])
and you run it on a command line like:
hello
argc = 1, argv[0] = "hello"
if you run it like
hello these are my arguments to the program
argc = 8
argv[0] = hello
argv[1] = these
argv[2] = are
argv[3] = my
argv[4] = arguments
argv[5] = to
argv[6] = the
argv[7] = program
When the Kernel runs the program, it passes the parameters and parameter info through these two parameters.
argc = Number of Arguments
argv[] = Array containing the arguments
as for void main()
just don't use it!
You will be flamed badly on these boards if you do.
read Salem's quote!
-LC
Another FAQ link that answers your questions:
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
I suggest you browse the whole FAQ so that you try to find answers to your questions before posting them here.
What is this anyway, void main week? :D
[edit]Even this is in the FAQ:
>>void main( ) is never legal. Period.<<
Argument passing is done by the shell.Quote:
Originally posted by Lynux-Penguin
When the Kernel runs the program, it passes the parameters and parameter info through these two parameters.
THe shell doesn't run the program, the kernel does
The shell tells the kernel to run it.
Read
Linux Core Kernel, 2nd Edition
And read about ELF, the same applies to almost every OS.
Correct me if I'm wrong. I just read a lot of forums, some of my information is opinion based, but I almost sure of this.
-LC