Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 7th, 2005, 11:16 AM   #1
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 138
Rep Power: 4 linuxpimp20 is on a distinguished road
noob questions about main() format

hi im doing some exercises in a book for c and have seen main() appear different ways oppose to the book and different websites. I want to make sure im typing main() correctly so i get in the habit of doing so all the time. (or set up my editor to so) so here are the questions:

1. does main always return an int to the enviroment to express success or failure and thus could always be typed as int main()?

2. does main ever recieve arguments (besides argc and *arcv[ ]) and thus could always be declared int main(void)?

3. is main() and main(void) the same thing? I have a feeling they are not but making sure.

if i mispoke at all please correct me. thanks for any replies in advance. i appreciate it.
linuxpimp20 is offline   Reply With Quote
Old Jul 7th, 2005, 11:34 AM   #2
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
main() is a the function that the program starts off with at excecution.

1. The return is like to "clear" everything out before closing the program.

2. Im not too sure it invlovles advance compiler use, but im assuming that their arguments for other source files to put together.

3. Yes. main(void) is more efficent though since you're saying that their is no arguments instead of just leaving it out.

Someone else will soon anwser question 2 with more detail.
Navid is offline   Reply With Quote
Old Jul 7th, 2005, 12:02 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by linuxpimp20
hi im doing some exercises in a book for c and have seen main() appear different ways oppose to the book and different websites. I want to make sure im typing main() correctly so i get in the habit of doing so all the time. (or set up my editor to so) so here are the questions:

1. does main always return an int to the enviroment to express success or failure and thus could always be typed as int main()?
Yes, always. main should NEVER ever be declared void main() even though you might find that in some books. Some compilers won't accept that.

Quote:
Originally Posted by linuxpimp20
2. does main ever recieve arguments (besides argc and *arcv[ ]) and thus could always be declared int main(void)?
There are three accepted ways to declare main. There is no performance penalty in using either the 2nd or 3d method over the first. The program's startup code always passes the two arguments whether you declare them in main() or not.
int main(int argc, char* argcv[]);
int main();
int main(void);

Some compilers, such as VC++ 6.0, will pass a third argument which is an array of environment strings. But that is non-ANSI standard.
int main(int argc, char* argcv[], char* env[]);
Quote:
Originally Posted by linuxpimp20
3. is main() and main(void) the same thing? I have a feeling they are not but making sure.
Depends on C or C++. In C, main() means a function with unspecified arguments, but main(void) is a function with no arguments. In c++ both are the same (no arguments).
Ancient Dragon is offline   Reply With Quote
Old Jul 7th, 2005, 2:32 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
1) yes, 2) yes/no, 3) yes
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jul 7th, 2005, 4:52 PM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by Infinite Recursion
1) yes, 2) yes/no, 3) yes
Well, you got a 66% on that test. the answer to 3) is maybe, depending on C or C++.
Ancient Dragon is offline   Reply With Quote
Old Jul 7th, 2005, 5:33 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
C and C++ differ just as Dragon says. The reason is that C will accept a function call without the function having been declared in advance and assume certain things. C++ will not allow that.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 7th, 2005, 6:48 PM   #7
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 138
Rep Power: 4 linuxpimp20 is on a distinguished road
Quote:
Originally Posted by DaWei
C and C++ differ just as Dragon says. The reason is that C will accept a function call without the function having been declared in advance and assume certain things. C++ will not allow that.
is that because c is a weak typed language and c++ is a strong typed language? i noticed that C can be not as strict at times.
linuxpimp20 is offline   Reply With Quote
Old Jul 8th, 2005, 9:19 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
<TANGENT>

Quote:
Originally Posted by Ancient Dragon
Well, you got a 66% on that test. the answer to 3) is maybe, depending on C or C++.

82.5%

Being that its maybe, implies a yes or no... since I answered yes, I was half right on the third question, which should supply a weight of half the possible points. At least I passed

</TANGENT>
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jul 7th, 2005, 5:50 PM   #9
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4 mackenga is on a distinguished road
1. does main always return an int to the enviroment to express success or failure and thus could always be typed as int main()?

To be pedantic, it doesn't necessarily always express success or failure; it's the 'exit status' of the program, and some programs use this for other things. A program that returns nonzero on successful exits that springs to mind is CHOICE.COM; it returns a number indicating which selection was made by its user.

On DOS the OS doesn't quite see an int as the exit status; the exit status of the program is accessible at the DOS prompt or in a batch file after the program exits as "errorlevel" and I seem to remember its range is more like an unsigned char.

2. does main ever recieve arguments (besides argc and *arcv[ ]) and thus could always be declared int main(void)?

If you mean int argc and char *argv[] (also known as char **argv, for reasons you'll probably discover later in your C career), then yes. argc is the number of arguments on the command line to the program, and argc[] is an array of pointers to the strings that are the arguments themselves. If your program never takes arguments it can declare main as void.

3. is main() and main(void) the same thing? I have a feeling they are not but making sure.

As others have answered before me, yes indeed it is. I'm not sure what the others mean by maybe, depending on whether it's C or C++; I was under the impression that C and C++ would both be happy with either, except for older C compilers which might want to you phrase quite a few things differently. I don't see what this has to do with C's lax attitude to implicit declarations; surely this is what means in C you could fail to specify the int return (since that's assumed as part of the implicit declaration anyway), rather than having anything to do with the explicit declaration of void arguments?
mackenga is offline   Reply With Quote
Old Jul 7th, 2005, 8:52 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
C is not a weakly typed language. C predates C++ by many years. The language writers decided that if a function was not declared or defined in advance, they would assume that certain defaults were implied. The writes of C++ decided that wasn't a good idea. Here's an example of the type of warning issued by a C compiler when a function is invoked without being declared or defined.
Compiling...
btree.c
c:\documents and settings\david\my documents\my windows\bobs\btree.c(77) : warning C4013: 'hooraw' undefined; assuming extern returning int

btree.obj - 0 error(s), 1 warning(s)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC