main problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shardul316
    New Member
    • Jan 2007
    • 20

    main problem

    dear friends plz clarify my doubt
    we know that there are three aspects regarding function
    1.function declaration
    2.function call
    3.function definition
    in c,c++ program at the start we write
    main()
    {
    ...;
    ...;
    }
    now my question is
    is this a function declaration or definition or calling?
    if it is a function call why don't we give semicolon after main
    since often we write function call as
    Ex.
    swap();
    add();
    then why not main();
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    this is a function definition. The function call is made from with-in the c-start-up code provided by the compiler writer. I do not think that main is declared anywhere.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      From what I know of the subject, the main reason for a function declaration is to let your compiler know that the function exists. If you didn't declare a function, but still had the definitions somewhere below your main, the compiler may get confused and not recognize these functions when it tries to compile main(). I'm not sure, but I think you could replace the function declarations at the beginning of your program with the full definition, so that the main() function recognizes its existance and already has the code.

      Comment

      • shardul316
        New Member
        • Jan 2007
        • 20

        #4
        Originally posted by Banfa
        this is a function definition. The function call is made from with-in the c-start-up code provided by the compiler writer. I do not think that main is declared anywhere.
        THANKS FOR THE REPLY.
        you mentioned that main is not declared.
        i agree with you in case of C .
        As in C function declaration is optional but as far as C++ is concerned
        function declaration is must.
        so how it is possible for compiler to compile program without declaration of function in C++.

        Comment

        • shardul316
          New Member
          • Jan 2007
          • 20

          #5
          Originally posted by Ganon11
          From what I know of the subject, the main reason for a function declaration is to let your compiler know that the function exists. If you didn't declare a function, but still had the definitions somewhere below your main, the compiler may get confused and not recognize these functions when it tries to compile main(). I'm not sure, but I think you could replace the function declarations at the beginning of your program with the full definition, so that the main() function recognizes its existance and already has the code.
          THANKS FOR THE REPLY.
          you mentioned that main is not declared.
          i agree with you in case of C .
          As in C function declaration is optional but as far as C++ is concerned
          function declaration is must.
          so how it is possible for compiler to compile program without declaration of function in C++.

          --------------------------------------------------------------------------------

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            It is possible because the piece of code that calls main is not (normally) released to you as source code (except for reference) but is realeased precompiled as binary code. The definition is only required for compilation of the function call not the function definition.

            I have checked my MSVC++ installation and main is predeclared, but only in an internal header that appears to be only included into library source code.


            The rule is C++ is not that a function must be predeclared. It is that a function can't not be called before it has been defined or predeclared. Which is subtly different.

            Comment

            Working...