Difference in initialising main

This is a discussion on Difference in initialising main within the C Programming forums, part of the General Programming Boards category; What’s the difference between: main() int main() void main() is there another way of initializing main? Thx...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    30

    Difference in initialising main

    What’s the difference between:
    main()
    int main()
    void main()


    is there another way of initializing main?

    Thx

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    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.....
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    the one other way of initializing main() is
    Code:
    int main(int argc,char *argv[])
    which makes use of the outside operating system, for things like command line arguments.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>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 )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    Thx. i've came across smething else

    int main(void)


    What's this?

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    Thx Salem. got another thing i don't quite understand

    int main(int argc,char *argv[])
    what does argc and *argv[] means n what do they do.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    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.
    argc is the number of arguments supplied to the program, *argv[] is the pointer to the argument strings.
    $ENV: FreeBSD, gcc, emacs

  9. #9
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    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.....
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>I've hurd that void main () is legal
    void main( ) is never legal. Period.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >>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*).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    int main(int argc, char* argv[])
    Lets say the program is called "hello"
    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
    Last edited by Lynux-Penguin; 07-24-2003 at 03:37 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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?

    [edit]Even this is in the FAQ:
    >>void main( ) is never legal. Period.<<
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Originally posted by Lynux-Penguin

    When the Kernel runs the program, it passes the parameters and parameter info through these two parameters.
    Argument passing is done by the shell.
    $ENV: FreeBSD, gcc, emacs

  15. #15
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Last edited by Lynux-Penguin; 07-24-2003 at 07:54 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-15-2008, 04:38 AM
  2. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  3. main() question
    By Encrypted in forum C Programming
    Replies: 5
    Last Post: 01-21-2003, 04:05 PM
  4. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 08:08 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 04:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21