Results 1 to 7 of 7

Thread: main()

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Answers
    9

    main()

    In some compilers void main() works but actually return type of main() is int, how does this work?


  2. #2
    Junior Member
    Join Date
    Aug 2007
    Answers
    4

    Re: main()

    Actually in C, every function retuns integer defaulty.
    But not every function must return integer.
    we can change the return type.
    Main is also an function, so we can change the return type.


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    9

    Re: main()

    Quote Originally Posted by chpushpakumar View Post
    Actually in C, every function retuns integer defaulty.
    But not every function must return integer.
    we can change the return type.
    Main is also an function, so we can change the return type.
    That is correct, but for that we need to change the prototype of the function as well and in case of main we can't do that, isn't it?


  4. #4
    Junior Member
    Join Date
    Aug 2007
    Answers
    4

    Re: main()

    In C, every program execution starts from main and ends with main. So the first function executed in main it self. So no need to prototye main function, if we change the return type.


  5. #5
    Junior Member
    Join Date
    Oct 2005
    Answers
    1

    Re: main()

    The summary as interpreted by me is something like this:-
    1. ANSI / ISO standards for C and C++ languages does not support void main()
    2. Compilers seem to allow this syntax, however with this syntax a random value is returned to the environment calling the C program.
    3. This syntax can cause scripts to fail, in case they depend on the return value of a program.
    4. This syntax can also cause stack corruption in some extreme cases

    So points to be well remembered are -
    1. definitely think whether the program is going to be executed by other programs / scripts etc - if so write int main() -
    2. also if you worry about cross-compiler compatibility of the programs or compliance to standards - use int main()
    3. Very simple rule of thumb - always use int main() except when there is some specific reason to use void main()


  6. #6
    Junior Member
    Join Date
    Jul 2006
    Answers
    1

    Re: main()

    Quote Originally Posted by pbchaudhari View Post
    In some compilers void main() works but actually return type of main() is int, how does this work?
    Default return data type of any function in Cpp is int ,it mean when you are not specify any datatype at that point compiler will set main() function with int as return datatype.(function type:no arg or with arg but with return value).but when you specify void as datatype at that point compiler will set with(function type:no arg or with arg but no retrun value).


  7. #7
    Junior Member
    Join Date
    Aug 2007
    Answers
    2

    Re: main()

    It can be over loaded ,also final.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact