![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 138
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#3 | |||
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4
![]() |
Quote:
Quote:
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:
|
|||
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 138
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() ![]() ![]() |
<TANGENT>
Quote:
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." |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|