PDA

View Full Version : about functions in c language


kthouz
Sep19-07, 04:09 AM
What is the effect of putting void before main function i mean?

void main()
{
}
void main(void)
{
}

D H
Sep19-07, 04:23 AM
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).

jim mcnamara
Sep20-07, 09:26 AM
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.

mattst88
Oct13-07, 08:41 PM
main() should be defined as 'int main()' or in many cases 'int main(int argc, char * argv[])'.

Hurkyl
Oct14-07, 03:06 AM
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.