Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

About functions in c language

  1. Sep 19, 2007 #1
    What is the effect of putting void before main function i mean?

    void main()
    void main(void)
  2. jcsd
  3. Sep 19, 2007 #2

    D H

    Staff: Mentor

    What you wrote means the function returns no value. This is valid syntax in general, but not for the main function. Many functions don't have a return value. For example, a function that receives a structure pointer as an argument and updates the contents of the structure might well have no return value.

    The main function must return an integer (an int) to be compliant with the standard. The returned value indicates success (zero) or failure of some sort (non-zero).
  4. Sep 20, 2007 #3

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    DH is completely right: void main() is not okay, 'not nowhow, not noway' as the Lion said to Dorothy.

    If you leave it as void main(), when the program returns to the shell or whoever called it, the return value can be any integer, ie. whatever garbage is left in stack space. If it returns a zero, it is probably pure luck - programming by coincidence.
  5. Oct 13, 2007 #4
    main() should be defined as 'int main()' or in many cases 'int main(int argc, char * argv[])'.
  6. Oct 14, 2007 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The C++ standard requires main to return int.

    The C standard permits compilers to allow other return types -- but such code is, in principle, nonportable.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?