+ 2
When is void main use. And when is int main used... plis help
and what is the difference between int main void main
2 Antworten
+ 1
void main is used when you don't want to return a value from function main() ...
int main() is used when you want to return an integer value at the end of a function main() ....
* int is integer data type.
* void is null data type.
consider studying C++ DATA TYPES & FUNCTION RETURN Methodology.
0
int as a data type for function main ()
is used to return an integer value back to the Operating System once the code is done executing, to let it know that it was executed successfully.
Example:
int main ()
{
std::cout << " Hello World!" << std::endl;
return 0;
}
Hello World! is printed to the screen. After execution, the value 0 is returned to the OS letting it know your code was successful.
No matter the program, your int main() function is required and is the first function to execute. If you were to use a void:
void output ();
int main ()
{
std::cout << " Hello World!" << std::endl;
output ();
}
void output ()
{
std::cout<< " You Suck!" <<std::endl;
}
In this case, if "void output ()" were to be "int output ()", it would return a value back to the main function once it is done being executed, instead of to the OS, but we do not need it to return any integer to main, so we declare a type of void , which means it has no return value. All it is doing is outputting text to the screen, then it goes back to where it left off from the main function and returns integer 0 to the OS.