1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Where does main belong to?

Discussion in 'C, C++, C#' started by dalinkwent6, Aug 16, 2014.

  1. dalinkwent6

    dalinkwent6 Junior Member

    Jun 30, 2013
    Likes Received:
    5th Dimension
    Java was my first language and finished a c++ intro course last semester. Something that i've been wondering about is where main is?
    I understand that in java it can be in any object you put it in with static methods, but where is main in c++. I've read some explanations
    but i think i am missing a keypoint that allows me to see the picture. Anyone care to elaborate for me?
  2. mypmmail

    mypmmail Junior Member

    Jan 31, 2008
    Likes Received:
    Not sure if I understand your question correctly.

    The main() in c++ is similar in the sense that it can be in any .cpp file you have created.

    Just like java which requires you to define the main as
    static void main(String[] args) {}

    C++ requires you to define the main, less strict with optional arguments, as
    int main() {}

    I'm primarily a java programmer, with limited C++ knowledge.
    Concept-wise, they are similar with Java having less hardcore manipulation of memory.
    But, with the understanding of pointers in C++ does allow you to better appreciate how Java does certain things.
  3. wolverine

    wolverine Newbie

    Jul 7, 2008
    Likes Received:
    I think you mean where main is written am i right? main is always written in a cpp source file, it's upto you whichever source file you put it into, that cpp file will be consider the main
  4. TAHA95

    TAHA95 Registered Member

    Oct 25, 2014
    Likes Received:
    If you're doing your code in just a single file without any separate files for headers and codes then the main is the last function of your code. It's at the bottom of your file but at run-time it's the first to be compiled. Any functions you write below the main will not be executed and you will get an error
  5. TrafficWizard

    TrafficWizard Junior Member

    Aug 22, 2014
    Likes Received:
    main(){} is essential function what you need to include in source file as in example console project. It can contain other function calls variables etc. if you done Java you know how things work around tho c++ is a f**** shotgun compared to java :)