Thread: void main()

  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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    >> 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!");
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    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,164
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10: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, 02:20 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03: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