Results 1 to 11 of 11

Thread: " 'void main() ' vs 'int main()' " and " 'getch();' vs 'return.0' "

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    7

    " '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)....
    Search message boards with MessageKing

  2. #2
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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>
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  3. #3
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    return 0 just exists the program if u start the program with an int return type...
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You should use main in this form
    Code:
    main( int argc, char *argv[ ], char *envp[ ] )
    {
    program-statements
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    amac
    Guest
    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.

  6. #6
    Zaei
    Guest
    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.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    aah I see. thx
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width