void main()

This is a discussion on void main() within the C Programming forums, part of the General Programming Boards category; There are a bunch of ways to make the main part of a program like int main() and int main(void) ...

  1. #1
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24

    void main()

    There are a bunch of ways to make the main part of a program like int main() and int main(void) and void main ect. What are the types and how are they used?
    --Seph

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    int main() // same as int main(void)

    - or -

    int main(int argc, char ** argv)

    Since all C functions return an int whenever you don't specify, you could also use:

    main()

    - or -

    main(int, char**)

    Remember:

    void main()

    - and -

    void main(int, char**)

    ...are not correct, since 'main' *always* returns an int.



    ITSA
    Socket Library!

  4. #4
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    When you start a program, you have todo includes like stdin.h, conio.h, string.h, and ect. How do you know when to use them.
    --Seph

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> How do you know when to use them?

    Whenever the compiler complains that you're using an undeclared function.

    When you're a newbie, it's often easiest to create a special header of your own that simply includes most of the standard headers:


    Code:
    // inc.h
    #ifndef INC_H
    #define INC_H
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    #endif // INC_H

    Then in your program you'd simply do:


    Code:
    #include <inc.h>
    
    int main()
    {
     printf("Hello world!");
    }



    ITSA
    Socket Library!

  6. #6
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    wouldnt that just waste memory though. Like if i never used anything from math.h, it would be included into the program anyway and that would make the program bigger.
    --Seph

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Go back to your last question then. You have to know what functions are located where. So open the headers and find out what functions are there. Then you'll know.



    ITSA
    Socket Library!

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    Originally posted by Seph_31
    wouldnt that just waste memory though. Like if i never used anything from math.h, it would be included into the program anyway and that would make the program bigger.
    I would've thought a savvy compiler would only include the bits your source was actually using, so if you include math.h and don't use anything from it, the compiler will use it to search for stuff (So compilation takes a few milliseconds longer) but the size of the compiled file would be no different than if you omitted it.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Originally posted by Sebastiani
    >> How do you know when to use them?

    Code:
    // inc.h
    #ifndef INC_H
    #define INC_H
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    #endif // INC_H
    Code:
    #include <inc.h>
    
    int main()
    {
     printf("Hello world!");
    }
    shouldn't it be #include "inc.h"
    is there a difference?
    i'm not sure, can somebody clarify?

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Code:
    #include
    is always between < and >

    No idea why, I'm novice also...hope to learn C by just simply reading the topics.

  11. #11
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    When using #include anything between < and > is taken to be in the default include directory of your compiler. If you use quation marks (") then it will look for the file in the same directory as the source file. When you make your own header file you include by using
    Code:
    #include "Inc.h"
    This is because it is in the same directory and not the default directory with all the standard header files.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  12. #12
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Originally posted by Sebastiani
    int main() // same as int main(void)
    Not true, it says the difference in the faq posted above.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  13. #13
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by sean345
    When using #include anything between < and > is taken to be in the default include directory of your compiler. If you use quation marks (") then it will look for the file in the same directory as the source file.
    Close. If you use quotation marks, it looks in the same directory, THEN the default include directory. But it doesn't usually matter, when quotes are used, its usually in the same dir .
    Do not make direct eye contact with me.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Going back-thread for a moment:

    Originally posted by Sebastiani
    Since all C functions return an int whenever you don't specify, you could also use:

    main()

    - or -

    main(int, char**)
    Just to note that this is no longer true in C.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by SMurf
    I would've thought a savvy compiler would only include the bits your source was actually using, so if you include math.h and don't use anything from it, the compiler will use it to search for stuff (So compilation takes a few milliseconds longer) but the size of the compiled file would be no different than if you omitted it.
    Try it and see. Post your results here, with the compiler used.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

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