0
getch ();
Can I write getch rather than typing return in my college we use getch and then close the program. .
4 Réponses
+ 2
If your main function has been declared as 'int main()', you will need to end it with a return-statement.
The reason some people use getch is that some console applications will execute and terminate so fast, that you have no chance to see what it outputs to the console window.
By adding a call to getch you make the application wait for userinput before terminating and closing the console window.
That way you get a chance to look at the output.
+ 1
Well that's actually kind of tricky, and a little counter intuitive, in my opinion.
int main() requires a return statement with an exit code.
"return 0;" means that your program terminated as it was supposed to.
If an error happened, you should end your program with a "return -1;" statement.
But for some odd reason, "int main()" can end WITHOUT a return statement, which will be equivalent to "return 0;".
So basically the following two examples will be the same:
Example 1:
#include <iostream>
#include <stdio.h>
int main()
{
std::cout << "Hello SoloLearn" << std::end;
getch();
return 0;
}
Example 2:
#include <iostream>
#include <stdio.h>
int main()
{
std::cout << "Hello SoloLearn" << std::end;
getch();
}
So you can in fact end the main function without the return statement, but it's kind of a cheat :-)
+ 1
Yes, sure
0
so if I have started with int main () I can only end this with return -statement ?. . can't type getch at the end of the programme.
And thanks for the help :-)