When to use these commands?

4

What criteria do I use to apply the getch(); , return0; , and system("pause") commands to the end of a program?

    
asked by anonymous 19.11.2018 / 16:06

2 answers

4

getch

The getch() returns the key that was entered by the user, it is commonly used in menus with switch .

return 0;

return is a reserved word of the C syntax and several other programming languages. When we create a function, we define what type of data we will return (integer, decimal, text, boolean, no return) , to return the value we use the command return to return the value that we want.

In your case you should be creating your codes inside the main function, it looks something like this:

int main()
{
    //Códigos...

    return 0;
}

This function of ours has the following signature:

int defines the return type of our function, in which case it is an integer value.

main is the name of our function (we can give the name we want).

return command to return a value. As noted by @Denis, the main function returns an integer value for the operating system and any non-zero value represents error, so we return zero to successfully terminate the application.

system ("pause")

It is the call of a function that aims to pause the execution of our program until the next action of the user.

    
19.11.2018 / 16:18
1

getch is a function derived from conio.h . It reads the key pressed by the user. One day, conio.h may have been something decent and handy on the wheel, but please do not use any more .

system is a system call. Something similar to running a command on the terminal. Its own use is an indication that its code has an OS-dependent part (as noted by Denis Rudnei de Souza here and here ). In case, on Windows systems, there is a command called pause , which expects the user to have an action. Read more .

return is what Pedro Paulo put into your answer . Just to give some extra details, check out a list of common error codes used to exit the program. Just note one thing: the program's exit code is used to a specific form of IPC , only accepting a single byte. According to this link , the return in practice is given as % 256 , so as the least significant byte returned .

    
19.11.2018 / 17:49