|
-
Sep 24th, 2001, 10:36 PM
#1
Thread Starter
New Member
" 'void main() ' vs 'int main()' " and " 'getch();' vs 'return.0' "
i've seen both, but only used void main() and getch(); as that has been all my high school has ever taught me. Is there any difference in the two? (i'm using borland 5.02 btw)....
-
Sep 24th, 2001, 10:51 PM
#2
Fanatic Member
if you know about functions, you will know that main is just another function... so:
void main(void) - is a function that returns nothing and takes in nothing.
int main(void) - is a function that returns an int and takes in nothing.
int main(char*,int) - is a function that returns an int and takes in a pointer to a char and an int
getch() - is only borland specific.
all it does is wait for a user to press <enter>
-
Sep 24th, 2001, 10:57 PM
#3
Fanatic Member
return 0 just exists the program if u start the program with an int return type...
-
Sep 25th, 2001, 06:23 AM
#4
Frenzied Member
You should use main in this form
Code:
main( int argc, char *argv[ ], char *envp[ ] )
{
program-statements
}
-
Sep 25th, 2001, 06:43 AM
#5
getch() - is only borland specific.
getch() is not borland specific... Its available where ever it is defined...
You can use it in VC++ by including conio.h or on pretty much any UNIX machine by including... curses.h or ncurses.h
all it does is wait for a user to press <enter>
Actually it retrieves a character from standard input (STDIN) without echo'ing it... although waiting for user to press enter is one use of it.
-
Sep 25th, 2001, 06:57 AM
#6
Use return 0 if it is at all possible. Sometimes the program will quit if you just return 0, so getchar() (the stdio version of getch()) is required. If it was me, I would probably hurt anyone who put that at the end of a program.
Z.
-
Sep 25th, 2001, 09:48 AM
#7
char* envp[] as argument to main is microsoft specific.
and the totally ansi version of main is:
int main (int argc, char* argv[]);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 25th, 2001, 10:23 AM
#8
Fanatic Member
Originally posted by CornedBee
char* envp[] as argument to main is microsoft specific.
and the totally ansi version of main is:
int main (int argc, char* argv[]);
it doesnt matter what the variable name is.
-
Sep 25th, 2001, 11:16 AM
#9
Monday Morning Lunatic
Actually...there's a very good reason why you should not use void main(...).
On Intel/Win32, return values from functions are normally passed through EAX (the register). So therefore, if you don't return a value it's not really a problem.
However, some systems return values on the stack, so if you haven't pushed a value, then it'll all be out of sync and gets very messy indeed.
Moral of the story, do what ANSI and ISO tell you to do
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 25th, 2001, 11:21 AM
#10
the variable name does not matter, but no system/compiler combo except win32/vc++ will fill anything into the third argument
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 25th, 2001, 12:38 PM
#11
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|