Better to use int main ( )?
I thought that since we have taken a discussion of void main and int main, we could now go one step further into the topic. Which one is better? Or which one should we use and why?
Void is generally used to declare functions that do not return values.
You might have had this question about where does main function return its value to? Generally, a return value has to be returned to the calling process. When you use int main ( ), the main function returns an integer to the operating system (since the OS is what calls the program and in turn the main function). So OS is the one that calls your main ( ) function. Returning a value from main ( ) is like an exit function. The value is returned to the operating system and the program ends.
What will the OS do with your returned value? (Answer for this was provided by my best computer friend, Joe Steeve.....)
Actually, the OS never wants to know what you return. The program which started your program might need to know. In any OS, a program when running is called a "process". When booting up, the operating system starts the first process (called "init" in UNIX systems). Thereafter the first process starts other processes such as a shell (shell is just a program which reads commands from the user and converts it to system calls). So when you write a program and execute it in the shell, the shell starts your program. So now, the shell is the parent process of your program (and your program is the child of the shell); Now, in the same way, suppose you want one of your programs to load another program to do a particular job and you want to know just whether the job was successful or not, then the OS get the exit code of the child process and gives it to the parent process; just like returning from a function. So it is a standard to give the exit code as '0' for a success and any non-zero integer for a error. When programming in C/C++ you can give this exit code when you return from the main function. So you've to declare main as 'int main()' to do that. If you declare it as 'void main( )' C++ wont allow you to set a value to be returned to your parent program. So the variable which should contain the return code will be filled by no one., which means that memory can be in any state (unpredictable). Hence you have no control on what value your parent process gets when your child program exits. This is not just for UNIX, it holds good for MSDOS and Windows too. In the UNIXes the shell is usually 'sh' or 'bash'. In MSDOS the shell is called 'Command.com'. In Windows the shell is'explorer.exe'.
Hence it's always better to use int main ( ) along with a return value at the end of the main ( ) function. A return value of zero indicates a normally terminated program. A non-zero value is used in case of errors. Of course this does not mean that a program written with void main ( ) won’t work; but it is better to avoid writing such programs.
Thus the first program you wrote
(First Program) becomes :
# include <iostream.h>
int main( )
{
char letter;
cout << “Enter any letter” ;
cin >>letter;
cout << “The letter you entered is : ” <<letter;
return 0;
}
All the other programs that I have written so far have used void main ( ). They will all work if you use int main ( ) with a return value as done in the above example.
I advise you to go on with int main ( ) rather than void main ( ) whenever you write C++ programs. In case any of my programs use void main ( ), kindly let me know. I'll change it. Always make use of int main ( ).
Go to the next section on : Types of Functions
or go back to Contents
Copyright © 2003 Sethu Subramanian All rights reserved.